none
How to retrieve attribute values from a xml file

    Question

  • Could you please answer me "How to retrieve attribute values from a xml file as well as to insert data into same xml file . Suppose my xml file is like this

    <?xml version="1.0"?>

    <info>

    <book id="b1" lang="en">

    <name>c++</name>

    <price>570</price>

    </book>

    <book id="b2" lang="en">

    <name>c#</name>

    <price>250</price>

    </book>

    </info>

     

    With Regards

    Tapan

    Wednesday, April 11, 2007 11:15 AM

Answers

  • To add data to the document you can use methods like CreateElement and AppendChild e.g. C# sample code

    Code Snippet

    XmlElement newBook = xmlDocument.CreateElement("book");
    newBook.SetAttribute("id", "b3");
    newBook.SetAttribute("lang", "en");
    XmlElement name = xmlDocument.CreateElement("name");
    name.InnerText = "JavaScript";
    newBook.AppendChild(name);
    XmlElement price = xmlDocument.CreateElement("price");
    price.InnerText = "22";
    newBook.AppendChild(price);
    xmlDocument.DocumentElement.AppendChild(newBook);
    xmlDocument.Save("file.xml");

     

    Wednesday, April 11, 2007 12:33 PM

All replies

  • If you want to manipulate an XML document then System.Xml.XmlDocument is the right choice so you could load the XML document and access attributes like this (C# sample):

    Code Snippet

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(@"file.xml");
    foreach (XmlElement book in xmlDocument.SelectNodes(@"info/book"))
    {
      // if you know attribute name simply use GetAttribute e.g.
      Console.WriteLine("id value: {0}.", book.GetAttribute("id"));
      // if you don't know attribute names you can loop e.g.
      foreach (XmlAttribute attribute in book.Attributes) {
        Console.WriteLine("attribute with name {0} has value {1}.", attribute.Name, attribute.Value);
      }
    }

     

    Wednesday, April 11, 2007 12:27 PM
  • To add data to the document you can use methods like CreateElement and AppendChild e.g. C# sample code

    Code Snippet

    XmlElement newBook = xmlDocument.CreateElement("book");
    newBook.SetAttribute("id", "b3");
    newBook.SetAttribute("lang", "en");
    XmlElement name = xmlDocument.CreateElement("name");
    name.InnerText = "JavaScript";
    newBook.AppendChild(name);
    XmlElement price = xmlDocument.CreateElement("price");
    price.InnerText = "22";
    newBook.AppendChild(price);
    xmlDocument.DocumentElement.AppendChild(newBook);
    xmlDocument.Save("file.xml");

     

    Wednesday, April 11, 2007 12:33 PM
  • <?xml version="1.0"?>

    <info>

    <book id="b1" lang="en">

    <name>c++</name>

    <price>570</price>

    </book>

    <book id="b2" lang="en">

    <name>c#</name>

    <price>250</price>

    </book>

    </info>

     

     

    On the basis of this question being asked i want to ask that if i have a sitution in which now i want to update the values in the XML file that i have already being written with value i have passed thru my application...

    suppose i pass the id, name and price to be updated where the id=b1...them how do i search in the XML file where that id is and then update the values related to that id....

    I suppose you have understood my question please provide me with an answer and please give a code so as to get a better idea....

     

    Thanx

    Thursday, October 18, 2007 5:33 AM