Pedro Lopes
Pedro Lopes

Reputation: 481

Convert XDocument to string without added /r /n

Hello I'm trying to find a way to convert my XDocument to a string so that I can send that back with an ajax call.

So I have a XDocument as following

   var xml = new XDocument(
            new XElement("Contract",
                new XAttribute("version", "1.0"),
                new XElement("child", "Hello World!")));

I'm able to convert this to a string doing the following

string i = xml.Document.ToString();

but but by doing this my xml document comes out as wrong since alof ot /r/n and alot of "/" get added overall. Is there a way to convert an XDocument to string without this added values?

The string I get

"<Contract version=\"1.0\">\r\n  <child>Hello World!</child>\r\n</Contract>"

Upvotes: 2

Views: 2861

Answers (1)

j.v.
j.v.

Reputation: 997

This should do the trick:

string i = xml.Document.ToString(SaveOptions.DisableFormatting);

Upvotes: 7

Related Questions