Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

How to update XML file in asp.net

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
mohan21_kumar
Asked:
mohan21_kumar
1 Solution
 
yasser_helmyCommented:
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
 
MogalManicCommented:
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
 
bdarleyCommented:
Why not use a dataset. I believe you can load the document into the dataset and work with it from there.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now