The System.Xml
namespace provides standards-based support for processing XML. This includes classes for reading, writing, and manipulating XML documents and streams. It supports XML 1.0, XPath, XSLT, and the Document Object Model (DOM) and offers a fast, forward-only stream-based approach through XmlReader
and XmlWriter
.
This example demonstrates how to read an XML file using XmlReader
:
using System; using System.Xml; public class XmlReaderExample { public static void Main(string[] args) { string xmlString = "<bookstore><book genre=\"fiction\"><title lang=\"en\">The Great Gatsby</title><author>F. Scott Fitzgerald</author></book></bookstore>"; using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlString))) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: Console.Write($"<{reader.Name}>"); if (reader.HasAttributes) { while (reader.MoveToNextAttribute()) { Console.Write($" {reader.Name}='{reader.Value}'"); } reader.MoveToElement(); } Console.WriteLine(); break; case XmlNodeType.Text: Console.WriteLine($" {reader.Value}"); break; case XmlNodeType.XmlDeclaration: case XmlNodeType.ProcessingInstruction: Console.WriteLine($"<?{reader.Name} {reader.Value}?>"); break; case XmlNodeType.Comment: Console.WriteLine($"<!-- {reader.Value} -->"); break; case XmlNodeType.EndElement: Console.WriteLine($"</{reader.Name}>"); break; } } } } }
Assembly Name: System.Xml.dll
Namespace: System.Xml
Version: .NET Framework 4.8 (example version)
Related APIs:
This documentation pertains to assemblies commonly found in the .NET Framework and .NET Core/.NET 5+ ecosystems. Specific class availability and behavior may vary slightly between different versions.