XmlReader reader = XmlReader.Create("example.xml"); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "book") { using (XmlReader subReader = reader.ReadSubtree()) { while (subReader.Read()) { Console.WriteLine(subReader.Name + ": " + subReader.Value); } } } }
string xml = "This example parses an XML string and uses ReadSubtree to traverse the subtree of each node with the "book" tag. The code prints out the name and value of each element within the subtree. Package/Library: System.Xml. In summary, System ReadSubtree is a useful method for parsing XML data and traversing the subtree of a specific element. It is included in the System.Xml namespace and part of the .NET framework."; using (XmlReader reader = XmlReader.Create(new StringReader(xml))) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "book") { using (XmlReader subReader = reader.ReadSubtree()) { while (subReader.Read()) { Console.WriteLine(subReader.Name + ": " + subReader.Value); } } } } } Harry Potter J.K. Rowling The Hobbit J.R.R. Tolkien