This quick tutorial helps you to create XML document using C#, this C# code, should help you in creating XML in MVC or  Web-Forms also, as C# code remains the same, if you are looking to create sitemap XML using C# in MVC, read the detailed article here "Create XML sitemap dynamically in ASP.NET MVC C#"

Let's begin creating simple XML file using Console application sample in C#.

Step 1: Open your Visual Studio, and navigate to "File"-> "New"-> "Project"-> Select "Windows Desktop"(Left pane) and "Console App" (right-pane) -> Give a name your project ("CreateXMLDocument") and click "OK"

Step 2: We will write the C# code to create XML document in "Program.cs", we can divide this step of creating XML into these steps:

  1. Create new XMLDocument object
  2. Give XML declaration using XmlDeclaration (We can skip this step, but it is good practice to  write it down)
  3. Create the Root Element and insert it
  4. Now, create all the elements of XML which you want in the XML document.
  5. Append the elements inside Root Element of XML document
  6. Save the Document

Here is the complete code of all of the above steps in C# Console application (Program.cs)

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

namespace CreateXMLDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            //Decalre a new XMLDocument object
            XmlDocument doc = new XmlDocument();

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

            //create the root element
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmlDeclaration, root);

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

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

            XmlElement element3 = doc.CreateElement(string.Empty, "level2", string.Empty);

            XmlText text1 = doc.CreateTextNode("Demo Text");

            element1.AppendChild(element2);
            element2.AppendChild(element3);
            element3.AppendChild(text1);
            

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

            doc.Save(Directory.GetCurrentDirectory() + "//document.xml");
        }
    }
}

Now you can build and excute the project, once it is successfully build, you can check the file "document.xml" is created inside "yourprojectfolder/CreateXMLDocument/bin/Debug/document.xml" here is the screenshot

c-sharp-create-xml-document-console-application-example-min.png

That's it, we are done, you can also create XML document using Linq, which will reduce code lines, above C# Code would be using linq as below

 XDocument doc = new XDocument( new XElement( "MainBody", 
                                           new XElement( "level1", 
                                               new XElement( "level2", "Demo text" ), 
                                               new XElement( "level2", "other text" ) ) ) );
        doc.Save(Directory.GetCurrentDirectory() + "//document.xml");

Looks more easy if you know Linq, also do not forget to add "System.Xml.Linq" as a reference in your Program.cs file.

As you can see from above methods, linq based is much easy and quicker if you want to create a big XML file.

Few more tips:

Loading XML File

//create XMLdocument elment
XmlDocument xmldoc= new XmlDocument();  
//load XML from the file system URL
xmldoc.LoadXml("C:\\URL\\document.xml");  

Finding a node in XML file

XmlNode node = doc.DocumentElement.SelectSingleNode("/Label1/Text");

OR

foreach(XmlNode node in doc.DocumentElement.ChildNodes){
   string text = node.InnerText; //or loop through its children as well
}

That's it.

You may also like to read:

Open and Read XML in C# (Examples using Linq, XMLReader, XMLDocument)

JWT Token Authentication using Web API in ASP.NET Core 6