Ignore whitespace while reading XML

后端 未结 1 1036
终归单人心
终归单人心 2021-01-15 13:58

I have an XML format with following format


    Value

This comes from an external datasource I cannot change. When

相关标签:
1条回答
  • 2021-01-15 14:40

    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.)

    0 讨论(0)
提交回复
热议问题