XmlReader reader = XmlReader.Create("data.xml"); while (reader.Read()) { if (reader.Name == "book") { reader.MoveToElement(); Console.WriteLine(reader.Depth); } }
XmlDocument doc = new XmlDocument(); doc.Load("data.xml"); XmlElement root = doc.DocumentElement; XmlNodeList books = root.GetElementsByTagName("book"); XmlElement firstBook = (XmlElement)books[0]; firstBook.MoveToElement(); Console.WriteLine(firstBook.Name);In this example, the XmlDocument class is used to load an XML document and the root element is retrieved. The "book" elements are then retrieved using GetElementsByTagName method and the first book element is selected. The MoveToElement method is called to move the cursor to the "book" element, and the Name property is used to output the element's name. Package library: System.Xml.XmlDocument.