C# XML Processing Techniques

XML (eXtensible Markup Lnaguage) is a text-based markup language used for storing and transporting data.

This article demonstrates how to perform CRUD (Create, Read, Update, Delete) operations on XML files using C#.

The XML manipulation class are located in the System.Xml namespace.

Let's start with practical code examples.

using System;
using System.Xml;

namespace XmlProcessingDemo
{
    class XmlCreator
    {
        static void Main(string[] args)
        {
            // Create XML document object
            XmlDocument xmlDoc = new XmlDocument();

            // Create XML declaration
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            
            // Add declaration to document
            xmlDoc.AppendChild(xmlDeclaration);
            
            // Create root element
            XmlElement rootElement = xmlDoc.CreateElement("Users");
            
            // Add attribute to root element
            rootElement.SetAttribute("Category", "SampleData");
            xmlDoc.AppendChild(rootElement);

            // Create child element
            XmlElement userElement = xmlDoc.CreateElement("User");
            
            // Add text content to element
            userElement.InnerXml = "SampleUser";
            rootElement.AppendChild(userElement);
            
            // Save document to file
            xmlDoc.Save("UserData.xml");

        }
    }
}

The output of this code will be:

<?xml version="1.0" encoding="UTF-8"?>
<UserData Category="SampleData">
  <User>SampleUser</User>
</UserData>

Now, let's clarify the difference between XmlElement.InnerXml and XmlElement.InnerText with a demonstration:

using System;
using System.Xml;

namespace XmlPropertyComparison
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create XML document object
            XmlDocument xmlDoc = new XmlDocument();

            // Create XML declaration
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            
            // Add declaration to document
            xmlDoc.AppendChild(xmlDeclaration);
            
            // Create root element
            XmlElement rootElement = xmlDoc.CreateElement("Users");
            xmlDoc.AppendChild(rootElement);

            // First example using InnerXml
            XmlElement userElement1 = xmlDoc.CreateElement("User");
            // InnerXml treats string as XML markup
            userElement1.InnerXml = "<Info>SampleUser</Info>";
            rootElement.AppendChild(userElement1);
            
            // Second example using InnerText
            XmlElement userElement2 = xmlDoc.CreateElement("User");
            // InnerText escapes special characters
            userElement2.InnerText = "<Info>SampleUser</Info>";
            
            // Add attribute to second element
            userElement2.SetAttribute("ID", "12345");
            rootElement.AppendChild(userElement2);
            
            // Save document to file
            xmlDoc.Save("UserComparison.xml");

        }
    }
}

The output demonstrates the key difference:

<?xml version="1.0" encoding="UTF-8"?>
<Users>
  <User>
    <Info>SampleUser</Info>
  </User>
  <User ID="12345">&lt;Info&gt;SampleUser&lt;/Info&gt;</User>
</Users>

As clearly shown, if the string contains XML tags, InnerXml will parse them as actual XML elements, while InnerText will escape the special characters.

Next, let's demonstrate reading XML documents:

using System;
using System.IO;
using System.Xml;

namespace XmlReadingDemo
{
    class XmlReader
    {
        static void Main(string[] args)
        {
            // Create XML document object
            XmlDocument xmlDoc = new XmlDocument();
            
            // Check if file exists
            if (File.Exists("UserData.xml"))
            {
                // Load XML from file
                xmlDoc.Load("UserData.xml");
                
                // Get root element
                XmlElement rootElement = xmlDoc.DocumentElement;

                // Get child nodes collection
                XmlNodeList nodeList = rootElement.ChildNodes;
                
                // Iterate through each child node
                foreach (XmlNode node in nodeList)
                {
                    Console.WriteLine($"Node Name: {node.Name}");
                    
                    // Display node attributes if any
                    if (node.Attributes != null)
                    {
                        foreach (XmlAttribute attribute in node.Attributes)
                        {
                            Console.WriteLine($"Attribute: {attribute.Name} = {attribute.Value}");
                        }
                    }
                    
                    // Display node text content
                    Console.WriteLine($"Text Content: {node.InnerText}");
                }
                
                Console.ReadKey();
            }
        }
    }
}

Tags: C# XML XmlDocument InnerXml InnerText

Posted on Sat, 07 Nov 2026 16:34:31 +0000 by moon 111