Solved

How to update XML file in asp.net

Posted on 2004-10-18
3
403 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 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