Create and Save an XML file in ASP.Net with C#.

In this asp.net article we will learn to create and save an xml file in asp.net using c# language.

First, Add name space for use XML Component.


using System.Xml.Linq;

Now, Design asp.net page with one Button control and one TextBox control.

Here, we create an xml file when click the button and save it to drive and display xml file in Texbox control in asp.net

The asp.net web page design is :

Create an XML file in asp.net

Create an XML file in asp.net

write below code at Button click event for create and save an xml file in asp.net

protected void Button1_Click(object sender, EventArgs e)
{
XDocument xDoc = new XDocument
(
new XDeclaration(“1.0″, “utf-8″, “yes”),
new XComment(“We have Successfully Created an XML Document Programmtically !!”),
new XElement(“UserDetail”,
new XElement(“User”,
new XAttribute(“UID”, “01″),
new XElement(“Name”, “Vaidehi”),
new XElement(“Address”, “Mumbai”)
),
new XElement(“User”,
new XAttribute(“UID”, “02″),
new XElement(“Name”, “Meera”),
new XElement(“Address”, “Surat”)
)
)
);

String xmlFile = Server.MapPath(“~/XML/User.xml”);
xDoc.Save(xmlFile);

XElement xFile = XElement.Load(xmlFile);
TextBox1.Text = xFile.ToString();
}

Create and Save an XML file in asp.net

Create and Save an XML file in asp.net

The created xml file look like :

Create and Save an XML file in asp.net

I hop this create and save an xml file example in asp.net will help you.

Leave a Reply

Your email address will not be published. Required fields are marked *

5 + 7 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">