Solved

How to update XML file in asp.net

Posted on 2004-10-18
3
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Simple Linear Regression
Introduction to Processes

617 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