Xml with spaces like InnerText

I am parsing XML data containing such entries:

<item name="UserText" type_name="gh_string" type_code="10">      </item>

I have to read 6 spaces as a String, but the InnerText and InnerXml values ​​for System.Xml.XmlNode are zero-length strings.

Is there any way to get whitespace data in existing files and what should I do in the future to prevent this kind of screw from appearing?

+3
source share
3 answers

XML ignores spaces. If you need to save it, you must insert the attribute xml:space="preserve"in your elements. Something like this, which I think will keep your spaces wherever XML is consumed.

<item xml:space="preserve" name="UserText" type_name="gh_string" type_code="10">      </item>

, LINQ-to-XML, PreserveWhitespace. :

XElement element = XElement.Parse(xml, LoadOptions.PreserveWhitespace);

CData, .

+4

xml XMLReader, XMLReaderSettings IgnoreWhitespace, false

XmlReader r = XmlReader.Create("file",new XmlReaderSettings{ IgnoreWhitespace=false;})
+3

If you use XMLDocument, install XMLDocument.PreserveWhitespace = truebefore calling Load.

See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.preservewhitespace.aspx

This works for me well.

+3
source

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


All Articles