Q:

How can I iterate on XML elements from an XML using XMLReader and bypass whitespaces?

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.

A:

XmlReader constructor has an overload that takes an XmlReaderSettings object.  The XmlReaderSettings object has an IgnoreWhitespace property.
In order to read only the next elements you can implement an extension method on XmlReader.
Here's an example:
public static class ExtensionMethods
{
    public static bool ReadNextElement(this XmlReader reader)
    {
        while (reader.Read())
            if (reader.NodeType == XmlNodeType.Element)
                return true;

        return false;
    }
}

And here's a little console application that will demonstrate this:
public class Program
{
    public static void Main(string[] args)
    {
        var settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
        settings.IgnoreComments = true;
        settings.IgnoreProcessingInstructions = true;

        var reader = XmlReader.Create("XMLFile1.xml", settings);
        while (reader.ReadNextElement())
            Console.WriteLine(reader.Name);
    }
}

