Login Register
Code2night
  • Home
  • Blog Archive
  • Tutorial
  • Interview Q&A
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. How to Create XML Documents in ASP.NET

How to Create XML Documents in ASP.NET

Date- Sep 30,2023

7200

Free Download Pay & Download
Xml Document Creating Xml Document

Creating XML Documents in ASP.NET

XML (Extensible Markup Language) is a widely used format for storing and transporting structured data. In ASP.NET, you can create, manipulate, and work with XML documents using the System.Xml namespace. This article will guide you through creating an XML document from scratch, adding nodes, child nodes, and attributes.

Step 1: Import the System.Xml Namespace

Before you can work with XML in your ASP.NET application, you need to import the System.Xml namespace:

using System.Xml;

Step 2: Create an XML Document

To create an XML document, you'll use the XmlDocument class. Here's how to create a basic XML document:

XmlDocument xmlDoc = new XmlDocument();

Step 3: Create the Root Element

Every XML document must have a root element. You can create one using the CreateElement method:

XmlElement rootElement = xmlDoc.CreateElement("Root");
xmlDoc.AppendChild(rootElement);

Step 4: Add Child Elements

You can add child elements to the root element using the CreateElement method:

XmlElement childElement1 = xmlDoc.CreateElement("Child1");
rootElement.AppendChild(childElement1);

XmlElement childElement2 = xmlDoc.CreateElement("Child2");
rootElement.AppendChild(childElement2);

Step 5: Add Text Content to Elements

You can set the text content of elements using the InnerText property:

childElement1.InnerText = "Hello, World!";
childElement2.InnerText = "ASP.NET XML Handling";

Step 6: Add Attributes

You can add attributes to elements using the CreateAttribute method:

XmlAttribute attribute1 = xmlDoc.CreateAttribute("Attribute1");
attribute1.Value = "Value1";
childElement1.Attributes.Append(attribute1);

XmlAttribute attribute2 = xmlDoc.CreateAttribute("Attribute2");
attribute2.Value = "Value2";
childElement2.Attributes.Append(attribute2);

Step 7: Save the XML Document

To save the XML document to a file or a stream, you can use the Save method:

xmlDoc.Save("example.xml");

Step 8: Load and Parse XML (Optional)

If you have an existing XML document and want to parse it, you can use the XmlDocument.Load method:

XmlDocument loadedDoc = new XmlDocument();
loadedDoc.Load("example.xml");

Step 9: Add Xml Declaration (Optional)

If you want to add xml declaration , you can add the following node

  XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            xmlDoc.AppendChild(xmlDeclaration);

This is the complete code for this

public ActionResult Index()
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            xmlDoc.AppendChild(xmlDeclaration);

            XmlElement rootElement = xmlDoc.CreateElement("Root");
            xmlDoc.AppendChild(rootElement);
            XmlElement childElement1 = xmlDoc.CreateElement("Child1");
            rootElement.AppendChild(childElement1);

            XmlElement childElement2 = xmlDoc.CreateElement("Child2");
            rootElement.AppendChild(childElement2);
            childElement1.InnerText = "Hello, World!";
            childElement2.InnerText = "ASP.NET XML Handling";
            XmlAttribute attribute1 = xmlDoc.CreateAttribute("Attribute1");
            attribute1.Value = "Value1";
            childElement1.Attributes.Append(attribute1);

            XmlAttribute attribute2 = xmlDoc.CreateAttribute("Attribute2");
            attribute2.Value = "Value2";
            childElement2.Attributes.Append(attribute2);
            xmlDoc.Save(Server.MapPath("example.xml"));
            return View();
        }

You can open the file in notepad and it will show following xml

Conclusion

In this article, we've explored how to create XML documents in ASP.NET, including creating elements, adding child elements, setting text content, and adding attributes. XML is a versatile format for representing structured data, and with the System.Xml namespace, you can easily create and manipulate XML documents in your ASP.NET applications.So this is how we can create xml documents in Asp.Net and add parent nodes and child nodes in xml document.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Implement Stripe Payment Gateway in ASP.NET: Complete Guide
Sep 10, 2020
Integrate jQuery Full Calendar with ASP.NET MVC: Step-by-Step
Sep 30, 2020
Retrieve Appointments from Microsoft Outlook Using ASP.NET MVC
Oct 03, 2020
How to Implement and Validate JWT Token Authentication in ASP.NET MVC
Oct 12, 2022
Previous in ASP.NET MVC
How to Integrate LinkedIn Login with OpenID Connect in ASP.NET MV…
Next in ASP.NET MVC
How to Convert Text to Speech in ASP.NET: Step-by-Step Guide

Comments

Contents

๐ŸŽฏ

Interview Prep

Ace your ASP.NET MVC interview with curated Q&As for all levels.

View ASP.NET MVC Interview Q&As

More in ASP.NET MVC

  • Integrate PayUMoney with ASP.NET MVC: Step-by-Step Guide 23120 views
  • CRUD Operations with Interfaces and Repository Pattern in AS… 21799 views
  • How to Use Ajax in ASP.NET MVC: Complete Guide 21157 views
  • Prevent Browser Reload When Saving Files in ASP.NET MVC 20580 views
  • Ultimate Guide to Exception Handling in ASP.NET MVC 20415 views
View all ASP.NET MVC posts โ†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy