Solved

How to update XML file in asp.net

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

830 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