Read particular depth XML ELEMENTS using XmlReader in C#
<p>I have an XML file with some nodes at different depths. I need a code to read only specific depth nodes but not all, and only by using XmlReader in C#. </p>

<p>Can someone help me in this? </p>

<p>Below is my XML structure. I want to read only "Depth2" nodes.</p>

<pre><code>&lt;Depth0&gt;
    &lt;Depth1&gt;
        &lt;Depth2/&gt;
        &lt;Depth2/&gt;
        &lt;Depth2/&gt;
    &lt;/Depth1&gt;
    &lt;Depth1&gt;
        &lt;Depth2/&gt;
        &lt;Depth2/&gt;
        &lt;Depth2/&gt;
    &lt;/Depth1&gt;
    &lt;Depth1&gt;
        &lt;Depth2/&gt;
        &lt;Depth2/&gt;
        &lt;Depth2/&gt;
    &lt;/Depth1&gt;
&lt;/Depth0&gt;
</code></pre>

<p>Code:</p>

<pre><code>using (var reader = XmlReader.Create("D:\\xyz.xml"))
{
    while (reader.Read())
    {
        if (reader.Depth == 4 &amp;&amp; reader.NodeType == XmlNodeType.Element)
        {
            XmlReader chnode = reader.ReadSubtree();
            AddItems(chnode);
        }
        else
            reader.MoveToElement();
       }
}
</code></pre>