How to update an xml file with XmlDocument?

Used Classes

XmlDocument
XmlNodeList

Before running the code our xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
  <Category ID="01">
    <MainCategory>XML</MainCategory>
    <Description>This is a list my XML articles.</Description>
    <Active>true</Active>
  </Category>
</CategoryList>

After running the code our xml becomes this:

<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
  <Category ID="01">
    <MainCategory>ASP.NET</MainCategory>
    <Description>This is now my ASP.NET list.</Description>
    <Active>false</Active>
  </Category>
</CategoryList>


The code which does that looks like this:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Page Language="C#" Debug="true" %>

<script  runat="server">
void Page_Load(object sender, System.EventArgs e){
    if(!Page.IsPostBack){
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("categories.xml"));

        XmlNodeList nodeList = xmlDoc.SelectNodes("/CategoryList/Category[@ID='01']");

        // update MainCategory 
        nodeList[0].ChildNodes[0].InnerText = "ASP.NET";
        // update Description 
        nodeList[0].ChildNodes[1].InnerText = "This is now my ASP.NET list.";
        // update Active 
        nodeList[0].ChildNodes[2].InnerText = "false";

        // Don't forget to save the file
        xmlDoc.Save(Server.MapPath("categories.xml"));
        Response.Write("XML File updated!");
    }
}
</script>

Filed under:

Comments

# TrackBack said:
Monday, May 17, 2004 1:36 PM
# TrackBack said:
Monday, May 17, 2004 1:36 PM
# Jerry Pisk said:

So much for using XML. Why exactly do you hardcode node positions? XML is meant to be extensible (that's what the X stands for) and by hardcoding node positions you're assuring your code is going to break.

Wednesday, May 19, 2004 5:44 PM
# Sonu Kapoor said:

Maybe you can show me how to correct the code ?

Sonu

Wednesday, May 19, 2004 5:47 PM
# Jerry Pisk said:

// update MainCategory
nodeList[0].SelectSingleNode("MainCategory ").InnerText = "ASP.NET";

Add error checking...

Thursday, May 20, 2004 4:01 PM
# Sonu Kapoor said:

Jerry you mean something like ?

if(nodeList.Count > 0)
{
// update Main Category
nodeList[0].SelectSingleNode("MainCategory ").InnerText = "ASP.NET";
}

Sonu

Thursday, May 20, 2004 7:28 PM
# Jerry Pisk said:

Actually, if you're looking to update a specific category by a supplied id:

void UpdateCategory(XmlDocument Doc, string CategoryId, string MainCategory...)
{
if( Doc == null )
{
return;
}
if( CategoryId == null || CategoryId.Length == 0 )
{
return;
}

string CategoryQuery = String.Format("/CategoryList/Category[@ID='{0}']", CategoryId);
XmlNode CategoryNode = Doc.SelectSingleNode(CategoryQuery);

if( CategoryNode == null )
{
return;
}
XmlNode MainCategoryNode = CategoryNode.SelectSingleNode("MainCategory");
if( MainCategoryNode != null )
{
MainCategoryNode.InnerText = MainCategory;
}
...
}

Thursday, May 20, 2004 7:38 PM
# Jerry Pisk said:

How can one post <pre> comments in .Text?

Thursday, May 20, 2004 7:38 PM
# Sonu Kapoor said:

Ah....I know now what you mean Jerry, many thanks for the information. I will update the entry with your reference in it. I am glad that somebody is here to help me with the "XML How to's".

I have no clue how the <pre> can be used in the comment section. I guess its not possible.

Sonu!

Thursday, May 20, 2004 7:42 PM
# Sotos said:

Hi!Can you suggest me the best way to add a new <Category ID="02"> node?

Tuesday, May 25, 2004 8:00 AM
# Sotos said:

The above article is very useful to start with XmlDocument...but i am a little confused on how to add a new <Category ID="02"> node
Thanks in advance

Tuesday, May 25, 2004 8:02 AM
# Sonu Kapoor said:

The above how to only shows how you can update the file. Please see the "how to" to create a xml file.

http://weblogs.asp.net/sonukapoor/articles/132945.aspx

This will solve your problem.

Sonu

Tuesday, May 25, 2004 1:16 PM
# Maurizio said:

Hi all,

just a simple question:

is there a way to show the data I pulled out from the xml file in alphabetical order?

Thanks and merry xmas!

Maurizio

Friday, December 21, 2007 8:07 AM
# Brij Mohan said:

This is really great.

Can we use same thing for Resx Updation?

Saturday, January 12, 2008 1:14 AM
# Mir said:

Thanks a lotttttttttttttttttttttt

Tuesday, February 12, 2008 6:41 AM
# Deepesh said:

How to display a XML file in HTML

Friday, June 20, 2008 3:17 AM
# cpms said:

Hi

How can add new node?

Wednesday, June 24, 2009 4:25 AM
# srinivasaprabhu said:

Nice code.Thnks for doing so.. mr.Sonukapoor.Abut adding new child & parent node??

Thursday, August 26, 2010 6:32 AM
# rajendar.paisa said:

good article

for more info www.4microsoftsolutions.com/.../Introduction-to-XML.aspx

Thursday, August 18, 2011 3:17 PM

Leave a Comment

(required) 
(required) 
(optional)
(required)