XmlReader reader = XmlReader.Create("example.xml"); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { reader.EnsureMarkup(); Console.WriteLine(reader.Name); } }
using System.Xml.Linq; XElement root = XElement.Load("example.xml"); foreach (XElement element in root.Elements()) { string value = element.Element("ChildElement").Value; Console.WriteLine(value); }This example uses the XElement class from the System.Xml.Linq package to read an XML file and print out the value of a child element. The EnsureMarkup method is not used in this example, as the XElement class automatically ensures that the current node is an element. The package library that contains the XmlReader class is System.Xml.