using System.Xml; class Program { static void Main() { // Create an instance of XmlTextReader to read from an XML file. using (XmlTextReader reader = new XmlTextReader("example.xml")) { // Read each element in the XML file. while (reader.Read()) { // Do something with each element. For example, display the name of the element. Console.WriteLine(reader.Name); } } } }
using System.IO; using System.Xml; class Program { static void Main() { // Create a stream containing XML data. MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("In this example, we are creating a MemoryStream object containing XML data and passing it to the constructor of the XmlTextReader class. We are then reading each element in the XML data using the Read() method of the XmlTextReader class. For each element that has a value, we are displaying the value using the Value property of the XmlTextReader. Package/library: System.Xml (part of the .NET framework)")); // Create an instance of XmlTextReader to read from the stream. using (XmlTextReader reader = new XmlTextReader(stream)) { // Read each element in the XML data. while (reader.Read()) { // Do something with each element. For example, display the value of any elements with a value. if (reader.NodeType == XmlNodeType.Element && reader.HasValue) { Console.WriteLine(reader.Value); } } } } } Value1 Value2