Export (0) Print
Expand All

XmlReader.Depth Property

When overridden in a derived class, gets the depth of the current node in the XML document.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public abstract int Depth { get; }

Property Value

Type: System.Int32
The depth of the current node in the XML document.


// Create the XML fragment to be parsed.
string xmlFrag =
    @"<book>
             <misc>
                 <style>paperback</style>
                 <pages>240</pages>
             </misc>
         </book>";

// Create the XmlNamespaceManager.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

// Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

StringBuilder output = new StringBuilder();

// Create the reader.
using (XmlReader reader = XmlReader.Create(new StringReader(xmlFrag), null, context))
{

    IXmlLineInfo lineInfo = ((IXmlLineInfo)reader);
    if (lineInfo.HasLineInfo())
    {

        // Parse the XML and display each node.
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    output.Append(reader.Depth + " " +
                        lineInfo.LineNumber + ", " +
                        lineInfo.LinePosition);
                    output.AppendLine("<" + reader.Name + ">");
                    break;
                case XmlNodeType.Text:
                    output.Append(reader.Depth + " " +
                        lineInfo.LineNumber + ", " +
                        lineInfo.LinePosition);
                    output.AppendLine("  " + reader.Value);
                    break;
                case XmlNodeType.EndElement:
                    output.Append(reader.Depth + " " +
                        lineInfo.LineNumber + ", " +
                        lineInfo.LinePosition);
                    output.AppendLine("</" + reader.Name + ">");
                    break;
            }
        }
    }

    // Close the reader.
}

// Display the output to the TextBlock control
OutputTextBlock.Text = output.ToString();


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft