How to Create XML Documents in ASP.NET | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

How to Create XML Documents in ASP.NET

Date- Sep 30,2023

4557

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.

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
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 | 1210
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 Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts