How to keep spaces in attribute values ​​when using XDocument?

I am processing an xml that contains tabs ("\ t") and line breaks ("\ n") in their attribute values. When I parse it with XDocument.Parse (), tabs and line breaks are converted to spaces, even with the LoadOptions.PreserveWhitespace parameter.

How can I get an XDocument with the original attribute values?

+3
source share
2 answers

I did not find a real solution, so it turned out quickly and dirty:

xml = xml.Replace("\t", "	").Replace("\r", "
");

better than nothing...

0
source

XmlTextReader xml-. :

string textToParse = "<e a=\"x\ty\rz\n\" />" ;
using (var sr = new StringReader(textToParse)) {
    using (var xr = new XmlTextReader(sr)) {
        var xd = XDocument.Load(xr);
        System.Console.WriteLine(xd.ToString());
    }
}

<e a="x&#x9;y&#xD;z&#xA;" />
+2

Source: https://habr.com/ru/post/1769751/


All Articles