Link to home
Start Free TrialLog in
Avatar of sneeri_c
sneeri_c

asked on

Add to XML File

I am using VB.NET 2005.

I need to be able to add to an XML File. If I have the following:

<?xml version="1.0" encoding="utf-8" ?>
<applicationlist>
     <app>
          <name>AC</name>
          <srcdir>c:\AC</srcdir>
      </app>
</applicationlist>

I want to add a new <app> section to this xml. So I then have:

<?xml version="1.0" encoding="utf-8" ?>
<applicationlist>
     <app>
          <name>AC</name>
          <srcdir>c:\AC</srcdir>
      </app>
     <app>
          <name>NewAppInfo</name>
          <srcdir>c:\Info</srcdir>
      </app>
</applicationlist>

Thanks for the help.
Avatar of pradeepsudharsan
pradeepsudharsan

Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<book ISBN='1-861001-57-5'>" & _
                "<title>Pride And Prejudice</title>" & _
                "</book>")      

    'Create a new attribute.
    Dim newAttr as XmlAttribute = doc.CreateAttribute("genre")
    newAttr.Value = "novel"

    'Create an attribute collection and add the new attribute
    'to the collection.  
    Dim attrColl as XmlAttributeCollection = doc.DocumentElement.Attributes
    attrColl.InsertAfter(newAttr, attrColl.ItemOf(0))

    Console.WriteLine("Display the modified XML...")
    Console.WriteLine(doc.OuterXml)
Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "</book>")
        Dim root As XmlNode = doc.DocumentElement
       
        'Create a new node.
        Dim elem As XmlElement = doc.CreateElement("price")
        elem.InnerText = "19.95"
       
        'Add the node to the document.
        root.InsertAfter(elem, root.FirstChild)
       
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
ASKER CERTIFIED SOLUTION
Avatar of Nandakumar Sakthivel
Nandakumar Sakthivel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial