System.Xml namespace includes several classes to work with XML files. We can create a XML file or work with already created file. We can add parent and child nodes and attributes to these nodes using classes in System.Xml namespace. I will some of classes in this article to insert data into XML file.
1.Open MS Visual Studio 2010
2.File > New > Website > Visual C# or Visual Basic > ASP.NET Empty Web Site
3.Select Web Location as File System and Click OK
4.From Menu, Website > Add New Item > Select Web Form and Click Add.
5.Drag and Drop a Button control and a Label Control or write code below in Default.aspx page
<h3>Click to create XML file and insert data</h3>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
6.Open code behind file and add following namespace in it
C#
using System.Xml;
VB.NET
Imports System.Xml
7.Write code below in Button click event.
C#
protected void Button1_Click(object sender, EventArgs e)
{
string file = Server.MapPath("Customers.xml");
XmlDocument doc = new XmlDocument();
XmlNode decNode = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(decNode);
XmlNode parentNode = doc.CreateElement("Customers");
doc.AppendChild(parentNode);
XmlNode childNode1 = doc.CreateElement("Customer");
XmlAttribute id = doc.CreateAttribute("id");
id.Value = "1";
childNode1.Attributes.Append(id);
parentNode.AppendChild(childNode1);
XmlElement childElement1 = doc.CreateElement("Name");
childElement1.InnerText = "James John";
childNode1.AppendChild(childElement1);
XmlElement childElement2 = doc.CreateElement("Phone");
childElement2.InnerText = "(206) 555-9857";
childNode1.AppendChild(childElement2);
XmlElement childElement3 = doc.CreateElement("City");
childElement3.InnerText = "Seattle";
childNode1.AppendChild(childElement3);
XmlElement childElement4 = doc.CreateElement("State");
childElement4.InnerText = "WA";
childNode1.AppendChild(childElement4);
XmlNode childNode2 = doc.CreateElement("Customer");
XmlAttribute id2 = doc.CreateAttribute("id");
id2.Value = "2";
childNode2.Attributes.Append(id2);
parentNode.AppendChild(childNode2);
XmlElement childElement5 = doc.CreateElement("Name");
childElement5.InnerText = "Philip Wilson";
childNode2.AppendChild(childElement5);
XmlElement childElement6 = doc.CreateElement("Phone");
childElement6.InnerText = "(206) 555-8122";
childNode2.AppendChild(childElement6);
XmlElement childElement7 = doc.CreateElement("City");
childElement7.InnerText = "Redmond";
childNode2.AppendChild(childElement7);
XmlElement childElement8 = doc.CreateElement("State");
childElement8.InnerText = "WA";
childNode2.AppendChild(childElement8);
doc.Save(file);
Label1.Text = "XML File Created and data inserted successfully";
}
VB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim file As String = Server.MapPath("Customers.xml")
Dim doc As New XmlDocument()
Dim decNode As XmlNode = doc.CreateXmlDeclaration("1.0", "utf-8", Nothing)
doc.AppendChild(decNode)
Dim parentNode As XmlNode = doc.CreateElement("Customers")
doc.AppendChild(parentNode)
Dim childNode1 As XmlNode = doc.CreateElement("Customer")
Dim id As XmlAttribute = doc.CreateAttribute("id")
id.Value = "1"
childNode1.Attributes.Append(id)
parentNode.AppendChild(childNode1)
Dim childElement1 As XmlElement = doc.CreateElement("Name")
childElement1.InnerText = "James John"
childNode1.AppendChild(childElement1)
Dim childElement2 As XmlElement = doc.CreateElement("Phone")
childElement2.InnerText = "(206) 555-9857"
childNode1.AppendChild(childElement2)
Dim childElement3 As XmlElement = doc.CreateElement("City")
childElement3.InnerText = "Seattle"
childNode1.AppendChild(childElement3)
Dim childElement4 As XmlElement = doc.CreateElement("State")
childElement4.InnerText = "WA"
childNode1.AppendChild(childElement4)
Dim childNode2 As XmlNode = doc.CreateElement("Customer")
Dim id2 As XmlAttribute = doc.CreateAttribute("id")
id2.Value = "2"
childNode2.Attributes.Append(id2)
parentNode.AppendChild(childNode2)
Dim childElement5 As XmlElement = doc.CreateElement("Name")
childElement5.InnerText = "Philip Wilson"
childNode2.AppendChild(childElement5)
Dim childElement6 As XmlElement = doc.CreateElement("Phone")
childElement6.InnerText = "(206) 555-8122"
childNode2.AppendChild(childElement6)
Dim childElement7 As XmlElement = doc.CreateElement("City")
childElement7.InnerText = "Redmond"
childNode2.AppendChild(childElement7)
Dim childElement8 As XmlElement = doc.CreateElement("State")
childElement8.InnerText = "WA"
childNode2.AppendChild(childElement8)
doc.Save(file)
Label1.Text = "XML File Created and data inserted successfully"
End Sub
First we have established file path then we have created an object of XmlDocument class. We have created XML declaration node using XmlNode class. This declaration node is appended to our XML document using XmlDocument class AppendChild() method. Then I have created a parent node using same XmlNode class and appended this node to our XML file. Then a child node “Customer” is created for “Customers” parent node and added an attribute of “id” to it using XmlAttribute class. Then we have created elements in our child node and appended these elements to child node. We have also created appended another child node for parent node “Customers” and add elements to this child node as well. At the end we have to save our XML file.
8.Browse the website and click on button to see result