I have an XML format with following format
Value
This comes from an external datasource I cannot change. When
Not working answer
This answer doesn't seem to work, but I'm leaving it for the moment to avoid anyone else suggesting it. I'll delete this if someone posts a better answer.
Did you try setting XmlReaderSettings.IgnoreWhitespace?
White space that is not considered to be significant includes spaces, tabs, and blank lines used to set apart the markup for greater readability. An example of this is white space in element content.
For some reason this doesn't affect ReadElementContentAsString
or even the Value
property of a text node.
Simple answer
You could just call Trim
:
string value = reader.ReadElementContentAsString().Trim();
That won't remove line breaks between contentful lines, of course... if you need to do that, you could always use string.Replace
.
(As I mentioned in the comment, I'd personally prefer using LINQ to XML than XmlReader
unless you're genuinely reading something too large to fit in memory, but that's a separate matter.)