0

I have an XElement object which I would like to get its value as string.

However, the ToString() method returns the xml tree with /r/n and spaces which I woild like to ignore.

Is there an attribute of XElement which retrieves the XML tree as string without unnecessary adding?

user3165438
  • 2,631
  • 7
  • 34
  • 54

2 Answers2

1

You can pass SaveOptions as parameter to ToString method of XElement.

var xml = XElement.Parse("<a><b></b><c><d></d></c></a>");

Console.WriteLine(xml.ToString(SaveOptions.DisableFormatting));

prints:

<a><b></b><c><d></d></c></a>

While just calling ToString() will indeed produce formatted result

Console.WriteLine(xml.ToString());

prints:

<a>
  <b></b>
  <c>
    <d></d>
  </c>
</a>
Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
-1

You can use XElement.InnerXml statement.