How to create xml document in C#

How to create xml document in C#

In C#, you can create an XML document using System.Xml or the LINQ-based API System.Xml.Linq. The most modern and readable approach is LINQ to XML.

1. Create XML using LINQ to XML (Recommended)

Example: Creating and saving an XML file

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        XElement students =
            new XElement("Students",
                new XElement("Student",
                    new XElement("Id", 1),
                    new XElement("Name", "Alice"),
                    new XElement("Department", "CS")
                ),
                new XElement("Student",
                    new XElement("Id", 2),
                    new XElement("Name", "Bob"),
                    new XElement("Department", "IT")
                )
            );

        students.Save("students.xml");

        Console.WriteLine("XML file created successfully.");
    }
}

Output (students.xml)

<Students>
  <Student>
    <Id>1</Id>
    <Name>Alice</Name>
    <Department>CS</Department>
  </Student>
  <Student>
    <Id>2</Id>
    <Name>Bob</Name>
    <Department>IT</Department>
  </Student>
</Students>

2. Create XML using XmlDocument (Traditional way)

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        XmlDocument doc = new XmlDocument();

        XmlElement root = doc.CreateElement("Students");
        doc.AppendChild(root);

        XmlElement student = doc.CreateElement("Student");

        XmlElement id = doc.CreateElement("Id");
        id.InnerText = "1";

        XmlElement name = doc.CreateElement("Name");
        name.InnerText = "Alice";

        XmlElement dept = doc.CreateElement("Department");
        dept.InnerText = "CS";

        student.AppendChild(id);
        student.AppendChild(name);
        student.AppendChild(dept);

        root.AppendChild(student);

        doc.Save("students.xml");

        Console.WriteLine("XML created using XmlDocument.");
    }
}

When to use what?

Use LINQ to XML if:

• You want clean and readable code
• You are building modern applications
• You work with dynamic XML

Use XmlDocument if:

• You maintain legacy systems
• You need DOM-style manipulation
• You work with older .NET codebases

Quick intuition

Creating XML in C# is like building a tree structure in memory and then saving it to a file — LINQ to XML makes this process much simpler and more readable.

Creating a specific XML file

<body>
  <level1>
    <level2>text</level2>
    <level2>other text</level2>
  </level1>
</body>

We will provide two different methods that can be used to create XML files in C# programming language.

• XMLDocument based approach

• LINQ based approach

Using XmlDocument to create XML files in C#

Required using statements:

using System;
using System.Xml;
using System.Xml.XmlDocument

 Example code to create XML document using XmlDocument and XmlElement classes:

XmlDocument doc = new XmlDocument( );

//(1) the xml declaration is recommended, but not mandatory
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration( "1.0", "UTF-8", null );
XmlElement root = doc.DocumentElement;
doc.InsertBefore( xmlDeclaration, root );

//(2) string.Empty makes cleaner code
XmlElement element1 = doc.CreateElement( string.Empty, "body", string.Empty );
doc.AppendChild( element1 );

XmlElement element2 = doc.CreateElement( string.Empty, "level1", string.Empty );
element1.AppendChild( element2 );

XmlElement element3 = doc.CreateElement( string.Empty, "level2", string.Empty );
XmlText text1 = doc.CreateTextNode( "text" );
element3.AppendChild( text1 );
element2.AppendChild( element3 );

XmlElement element4 = doc.CreateElement( string.Empty, "level2", string.Empty );
XmlText text2 = doc.CreateTextNode( "other text" );
element4.AppendChild( text2 );
element2.AppendChild( element4 );

doc.Save( "D:\\xml-document.xml" );

Using LINQ to XML to create XML documents in C#

Required using statements:

using System;
using System.Xml.Linq;

Example code to create XML document using XDocument and XElement classes:

XDocument doc = new XDocument(
    new XElement( "body",
    new XElement( "level1",
    new XElement( "level2", "text" ),
    new XElement( "level2", "other text" )
)));
doc.Save( "D:\\xml-document.xml" );

Contents related to 'How to create xml document in C#'

How to Create and Write Excel File in C#
How to Create and Write Excel File in C#
How to Open and Read From Excel File in C#
How to Open and Read From Excel File in C#
Generate object from xml in c#
Generate object from xml in c#