Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to change and update XML file in ASP.NET

Author: Mike Green
Download Source Code : 1341_UpdateXMLFile.zip

I will explain in this article that how you can change and update XML file.

 

XmlDocument class is used to select or get nodes in XML file to do any changes and later save these changes to update the 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. Name of the Web Form will be Default.aspx
  5. Drag and Drop a Button control and a Label Control or write code below in Default.aspx page

    <
    <asp:Button ID="Button1" runat="server" Text="Read XML File"
                onclick="Button1_Click" />
    <br />
    <asp:Label ID="Label1" runat="server" ></asp:Label>
     
  6. From Menu, Website > Add New Item > Select XML File and Click Add. Rename the XML File as Customers.xml
  7. Open Customers.xml and write code below in it

    <?xml version="1.0" encoding="utf-8"?>
    <Customers>
      <Customer ID="1">
        <Name>Jaime</Name>
        <Phone>(415) 555-5938</Phone>
        <City>San Francisco</City>
        <Country>USA</Country>
      </Customer>
      <Customer ID="2">
        <Name>Horst</Name>
        <Phone>0372-035188</Phone>
        <City>Cunewalde</City>
        <Country>Germany</Country>
      </Customer>
    </Customers>
     
    Customer tag has ID associated to it. Each customer node has Name, Phone, City and Country child nodes. 

     
  8. Open code behind file and add following namespace in it

    C#
     
    using System.Xml;

    VB.NET

    Imports System.Xml
     
  9. Write code below in Button click event.

    C#
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/Customers.xml"));
     
        XmlNodeList nodes = doc.SelectNodes("/Customers/Customer[@ID='1']");
        nodes[0].ChildNodes[0].InnerText = "Jaime Yorres";
        nodes[0].ChildNodes[2].InnerText = "Portland";
     
        XmlNode node = doc.SelectSingleNode("/Customers/Customer[@ID='2']/Country");
        node.InnerText = "France";
     
        XmlNodeList nodes2 = doc.GetElementsByTagName("Customer");
        nodes2[1].ChildNodes[0].InnerText = "Horst Kloss";
     
        doc.Save(Server.MapPath("~/Customers.xml"));
     
        Label1.Text = "XML file is updated";
     
    }
     
    VB.NET
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim doc As New XmlDocument()
        doc.Load(Server.MapPath("~/Customers.xml"))
     
        Dim nodes As XmlNodeList = doc.SelectNodes("/Customers/Customer[@ID='1']")
        nodes(0).ChildNodes(0).InnerText = "Jaime Yorres"
        nodes(0).ChildNodes(2).InnerText = "Portland"
     
        Dim node As XmlNode = doc.SelectSingleNode("/Customers/Customer[@ID='2']/Country")
        node.InnerText = "France"
     
        Dim nodes2 As XmlNodeList = doc.GetElementsByTagName("Customer")
        nodes2(1).ChildNodes(0).InnerText = "Horst Kloss"
     
        doc.Save(Server.MapPath("~/Customers.xml"))
     
        Label1.Text = "XML file is updated"
    End Sub
     
    First we have created an object of XmlDocument class. XmlDocument class has Load() method which loads the XML file specified as parameter. You can get tags and nodes of XML files with different ways. One way is to call the SelectNodes() method of XmlDocument class. You can also call SelectSingleNode() method which selects single node. Another way to get nodes or tags is to call the GetElementByTagName() method of XmlDocument class. GetElementById() method selects single tag. You can change the inner text of the nodes by setting InnerText property. At the end don’t forget to call the Save() method to update changes in the XML file.
     
  10. Browse the website and click on button to see result
  11. Open Customers.xml again and you will see following data in it

     
    ?xml version="1.0" encoding="utf-8"?>
    <Customers>
      <Customer ID="1">
        <Name>Jaime Yorres</Name>
        <Phone>(415) 555-5938</Phone>
        <City>Portland</City>
        <Country>USA</Country>
      </Customer>
      <Customer ID="2">
        <Name>Horst Kloss</Name>
        <Phone>0372-035188</Phone>
        <City>Cunewalde</City>
        <Country>France</Country>
      </Customer>
    </Customers>

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< XML Serialization and Deserialization in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net