Friend, XML updating can be done something like this
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>
// 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>
Also another alternative is
As I understand you need to add/delete/update the xml file.
A simple way is to load xml into DataSet then bind the DataSet to GridView so you can edit XML files using GridView.
Above article shows how to edit xml-files using a GridView control. Additional the user may create new records using some text fields and a button.
And also you can load xml document into XmlDocument and XPath to update xml files here is an example:
/Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<markers> </markers>");
//Create a new node and add it to the document.
//The text node is the content of the price element.
XmlElement elem = doc.CreateElement("marker");
XmlAttribute attr1 = doc.CreateAttribute("lat");
attr1.Value = "37.441";
XmlAttribute attr2 = doc.CreateAttribute("lng");
attr2.Value = "-122.141";
elem.SetAttributeNode(attr1);
elem.SetAttributeNode(attr2);
doc.DocumentElement.AppendChild(elem);
Console.WriteLine("Display the modified XML...");
doc.Save("c:/add.xml");
//remove the added element and save to modified.xml
XmlElement root = doc.DocumentElement;
root.RemoveChild(elem);
doc.Save("c:/modified.xml");
Member
151 Points
473 Posts
Update XML file
Sep 28, 2013 03:00 PM|mehr_83|LINK
Hello,
My XML file is:
I want update value of loc , changefreq , priority tag where loc="http://test.com/12"
Please help me and please send me a sample code,
Thank you.
Star
10830 Points
2409 Posts
Re: Update XML file
Sep 28, 2013 05:13 PM|shabirhakim1|LINK
8510 Points
1056 Posts
<div class="comment-right-col">Re: Read XML file and Update C#. How to do it simple?
09-18-2008 09:03 AM|LINK
<div>Friend, XML updating can be done something like this
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>
Also another alternative is
As I understand you need to add/delete/update the xml file.
A simple way is to load xml into DataSet then bind the DataSet to GridView so you can edit XML files using GridView.
Here is sample code:
http://www.codeproject.com/useritems/Edit_Xml.asp
Above article shows how to edit xml-files using a GridView control. Additional the user may create new records using some text fields and a button.
And also you can load xml document into XmlDocument and XPath to update xml files here is an example:
Also you can try this one : http://www.eggheadcafe.com/community/aspnet/17/10018886/use-xpathnavigator-to-ite.aspx
Good luck!
</div> </div>Member
151 Points
473 Posts
Re: Update XML file
Sep 29, 2013 06:51 AM|mehr_83|LINK
Unfortunately i can not use this code, please help me,