Microsoft Docs

System.Xml Namespace

Overview

The System.Xml namespace provides standards-based support for processing XML. It includes classes for reading, writing, navigating, and validating XML documents.

Key concepts:

Classes

NameSummary
XmlReaderProvides a fast, forward-only way to read XML data.
XmlWriterWrites XML markup without needing to know underlying format details.
XmlDocumentRepresents an XML document and provides DOM manipulation methods.
XmlSerializerSerializes objects into XML streams and deserializes XML back into objects.
XmlElementRepresents an element in an XML document.
XmlAttributeRepresents an attribute of an XML element.
XmlNamespaceManagerManages namespaces in XML documents.

Example: Reading an XML File


using System;
using System.Xml;

class Program
{
    static void Main()
    {
        using (XmlReader reader = XmlReader.Create("books.xml"))
        {
            while (reader.Read())
            {
                if (reader.IsStartElement() && reader.Name == "title")
                {
                    string title = reader.ReadElementContentAsString();
                    Console.WriteLine($"Title: {title}");
                }
            }
        }
    }
}