Let say you have an XML like this:
<?xml version="1.0" encoding="utf-8"?>
<Class HashCode="307960707">
<Person>
<Class HashCode="-2020100801">
<FullName>
<FirstName>Dan</FirstName>
<LastName>K</LastName>
</FullName>
</Class>
<Age>20</Age>
<Class HashCode="-439631396">
<Address>
<Street>abc</Street>
<City>new york</City>
<ZipCode>30500</ZipCode>
<PhoneNumber>1245</PhoneNumber>
</Address>
</Class>
<Class HashCode="-1436395737">
<Person>
<Class HashCode="-1303968324">
<FullName>
<FirstName>katty</FirstName>
<LastName>G</LastName>
</FullName>
</Class>
<Age>18</Age>
<Class HashCode="-439631396">
<Address />
</Class>
<Class HashCode="307960707">
<Person />
</Class>
</Person>
</Class>
I want to be able to iterate only elements with XMLReader in the order they appear, which means class->Person-> class->FullName ,etc..
I was trying to navigate with methods like XMLReader.ReadStartElement() and it didn’t work especially when I read a whitespaces like "\n" which appears to be an element also. :/
I was trying to bypass that whitespace with method XMLReader.Read() with no success.
Please help me understand how should I navigate that way.
XmlReaderconstructor has an overload that takes anXmlReaderSettingsobject. TheXmlReaderSettingsobject has anIgnoreWhitespaceproperty.In order to read only the next elements you can implement an extension method on XmlReader.
Here’s an example:
And here’s a little console application that will demonstrate this: