Solved

How to update XML file in asp.net

Posted on 2004-10-18
3
393 Views
Last Modified: 2010-04-17
Hi,
How to update xml file through ASP.NET. For Example I have a XML File with the following details

- <Suppliers>
- <Provider>
  <UserID>0244</UserID>
  <Name>BillNetworks</Name>
  <Address1>Sathasivam Nagar</Address1>
  <Address1>Shivaji Nagar</Address1>
  <City>Bangalore</City>
  <State>Karnataka</State>
  <ZIP>530123</ZIP>
  <TaxID>11-1111111</TaxID>
  </Provider>
- <Provider>
  <UserID>12345</UserID>
  <Address1>BDA Layour</Address1>
  <Address2>AirportRoad</Address2>
  <City>Bangalore</City>
  <State>Karnataka</State>
  <ZIP>560 017</ZIP>
  <TaxID>12-11112222</TaxID>
  </Provider>
</suppliers>

I want to update this xml file corresponding to the userid tag. for example i want to update the address1 and addreess2 tag for userID 12345.

suggest me how to do this, give me some sample example that would reach my requirement


thanx
0
Comment
Question by:mohan21_kumar
3 Comments
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 12337228
use the System.Xml.XmlDocument class..
you can use LoadXml() method to load the xml string into the object..
then use SelectNodes() method to get all nodes that match the given criteria..
as an example:

XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlstr);
XmlNodeList lst = xml.xml.SelectNodes("/*/Provider[UserID='12345']");
foreach(XmlNode node in lst)
{
  node.SelectSingleNode("Address1").InnerText = "1st Address";
}

you may need to to learn about xpath
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 25 total points
ID: 12337328
There are two design patterns that I have used.

  1) Have a Template.XML document that defines the generic XML format and do code like this:
        XmlDocument tempXML= new XmlDocument();
        XmlDocument outXML = new XmlDocument();
        tempXML.LoadXml(Server.mapPath("template.xml"));
        outXML.load(tempXML.xml);
        XmlNode tempNode= outXML.xml.SelectSingleNode("/Suppliers/Provider");
        tempNode.selectSingleNode("UserID").text=???
        tempNode.selectSingleNode("Name").text=???
        tempNode.selectSingleNode("Address").text=???
        ...
        outXML.save();

  2) Write an XSLT stylesheet that adds/deletes/updates nodes of an XML document
        XmlDocument XML= new XmlDocument();
        XmlDocument style = new XmlDocument();
        style.loadXML("ManageXML.xsl");
        XML.loadXML("MyXMLFile.xml");
        /*Pass parameters to stylesheet (I'm not sure on the actual syntax for ASP.net)*/
        style.setParameter('action', 'addNode')
        style.setParameter('userID', ???);
        style.setParameter('Name, ???);
        style.setParameter('Address1', ???);
...
        results=XML.transform(style);
        results.save("MyXMLFile.xml");


0
 

Expert Comment

by:bdarley
ID: 12340799
Why not use a dataset. I believe you can load the document into the dataset and work with it from there.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now