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:
- Streaming reading/writing with XmlReader and XmlWriter.
- DOM-style manipulation using XmlDocument, XmlElement, and XmlAttribute.
- Serialization with XmlSerializer.
Classes
| Name | Summary |
|---|---|
| XmlReader | Provides a fast, forward-only way to read XML data. |
| XmlWriter | Writes XML markup without needing to know underlying format details. |
| XmlDocument | Represents an XML document and provides DOM manipulation methods. |
| XmlSerializer | Serializes objects into XML streams and deserializes XML back into objects. |
| XmlElement | Represents an element in an XML document. |
| XmlAttribute | Represents an attribute of an XML element. |
| XmlNamespaceManager | Manages 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}");
}
}
}
}
}