Try it yourself with our free Xml Formatter tool — runs entirely in your browser, no signup needed.

How to Parse XML in C#

How to Parse XML in C#

Parsing XML in C# is a crucial task for many applications, as it allows you to extract and manipulate data from XML files or strings. With the built-in System.Xml namespace, C# provides a robust and efficient way to parse XML. In this guide, we'll explore the most common use case, walk through the code, and cover edge cases, common mistakes, performance tips, and frequently asked questions.

Quick Example

Here's a minimal example that parses an XML string and extracts the value of a specific node:

using System.Xml;

string xmlString = "<root><name>John</name></root>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
XmlNode nameNode = xmlDoc.SelectSingleNode("//name");
string nameValue = nameNode.InnerText;
Console.WriteLine(nameValue); // Output: John

Step-by-Step Breakdown

Let's dissect the code:

  1. using System.Xml;: We import the System.Xml namespace, which provides the necessary classes for XML parsing.
  2. string xmlString = "<root><name>John</name></root>";: We define an XML string containing a simple document with a single node (name).
  3. XmlDocument xmlDoc = new XmlDocument();: We create an instance of the XmlDocument class, which represents the XML document.
  4. xmlDoc.LoadXml(xmlString);: We load the XML string into the XmlDocument instance using the LoadXml method.
  5. XmlNode nameNode = xmlDoc.SelectSingleNode("//name");: We use the SelectSingleNode method to select the first node with the name name using an XPath expression (//name). The // notation indicates that we're searching for a node anywhere in the document.
  6. string nameValue = nameNode.InnerText;: We extract the text content of the name node using the InnerText property.
  7. Console.WriteLine(nameValue);: We print the extracted value to the console.

Handling Edge Cases

Empty/Null Input

When handling empty or null input, you should check for these conditions before attempting to parse the XML:

string xmlString = null;
if (!string.IsNullOrEmpty(xmlString))
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlString);
    // ...
}
else
{
    Console.WriteLine("Invalid input");
}

Invalid Input

If the input XML is invalid, the LoadXml method will throw an XmlException. You can catch this exception and handle it accordingly:

try
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlString);
}
catch (XmlException ex)
{
    Console.WriteLine($"Invalid XML: {ex.Message}");
}

Large Input

When dealing with large XML files, consider using the XmlReader class instead of loading the entire document into memory:

using (XmlReader reader = XmlReader.Create("large.xml"))
{
    while (reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element && reader.Name == "name")
        {
            string nameValue = reader.ReadInnerXml();
            Console.WriteLine(nameValue);
        }
    }
}

Unicode/Special Characters

C# supports Unicode and special characters in XML strings. However, when working with XML files, ensure that the file encoding matches the encoding specified in the XML declaration:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("unicode.xml"); // Ensure the file is saved with the correct encoding

Common Mistakes

1. Not checking for null input

// Wrong
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(null);

// Correct
if (!string.IsNullOrEmpty(xmlString))
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlString);
}

2. Not handling invalid input

// Wrong
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<invalid xml>");

// Correct
try
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlString);
}
catch (XmlException ex)
{
    Console.WriteLine($"Invalid XML: {ex.Message}");
}

3. Not using the correct XPath expression

// Wrong
XmlNode nameNode = xmlDoc.SelectSingleNode("/name");

// Correct
XmlNode nameNode = xmlDoc.SelectSingleNode("//name");

Performance Tips

1. Use XmlReader for large files

When working with large XML files, use the XmlReader class to read the file in a streaming fashion, rather than loading the entire document into memory.

2. Use SelectSingleNode instead of SelectNodes

When selecting a single node, use the SelectSingleNode method instead of SelectNodes, which returns a collection of nodes.

3. Avoid using InnerText for large nodes

When working with large nodes, avoid using the InnerText property, which can lead to performance issues. Instead, use the ReadInnerXml method or iterate over the node's child nodes.

FAQ

Q: What is the difference between XmlDocument and XDocument?

A: XmlDocument is a legacy class that represents an XML document, while XDocument is a newer class that provides a more efficient and flexible way to work with XML. XDocument is recommended for new projects.

Q: How do I parse an XML file with a specific encoding?

A: Use the XmlReader class and specify the encoding when creating the reader: XmlReader.Create("file.xml", new XmlReaderSettings { Encoding = Encoding.UTF8 });

Q: Can I use XPath expressions with XDocument?

A: Yes, XDocument supports XPath expressions using the XPathSelectElements and XPathSelectElement methods.

Q: How do I handle XML comments in my parser?

A: XML comments are ignored by the parser, so you don't need to handle them explicitly.

Q: Can I use this parser with XML schemas?

A: Yes, you can use the XmlSchema class to validate your XML against a schema.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp