Hello. There is an XML document that is parsed with the XDocument.Parse() method. Then you need to translate it into a data type ( string ). I make the front method: XDocument.Parse().ToString() and output it to the Console.WriteLine() console, but this is displayed:

 <iq to="masterserver@k01/custom_server" id="uid00000068" type="get" from="5@k01/Client" xmlns="jabber:client"> <query xmlns="urn:cryonline:k01"> <setcurrentclass current="4" /> </query> </iq> 

Data is transferred from the server to the client, and this XML style is preserved.

But this document needs to lose its style:

 <iq to="masterserver@k01/custom_server" id="uid00000068" type="get" from="5@k01/Client" xmlns="jabber:client"><query xmlns="urn:cryonline:k01"><setcurrentclass current="4" /></query></iq> 

How can you do this?

  • one
    new Regex(@"\r?\n\s*").Replace(text, "") ? - VladD pm

2 answers 2

XDocument has a special ToString() overload that accepts SaveOptions , just use this:

 Console.WriteLine(XDocument.Parse(text).ToString(SaveOptions.DisableFormatting)); 
  • Oh cool! Did not find outright. - VladD

The best place to save is from an XDocument , specifying formatting options:

 var sb = new StringBuilder(); using (var w = new StringWriter(sb)) xd.Save(w, SaveOptions.DisableFormatting); var result = sb.ToString(); 
  • Thank you for your method that you left at the beginning, but it turned out to be a bit of a crutch). Andrei’s method I think suits more :) - Ilya Petrov
  • @ Ilya Petrov: Yes, his way is much more elegant. - VladD