Solved

Add to XML File

Posted on 2006-06-16
3
254 Views
Last Modified: 2013-11-19
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.
0
Comment
Question by:sneeri_c
  • 2
3 Comments
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16920585
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)
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16920594
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)
0
 
LVL 6

Accepted Solution

by:
Nandakumar Sakthivel earned 500 total points
ID: 16926092
Hi,
    You can use this if you want to save the data in xml file

            Dim childelement As XmlElement
            Dim childelement1 As XmlElement
            Dim xmldoc As New XmlDocument

            xmldoc.Load(<file name>)
            childelement = xmldoc.CreateElement("app")

            childelement1 = xmldoc.CreateElement("name")
            childelement1.InnerText = <value>
            childelement.AppendChild(childelement1)

            childelement1 = xmldoc.CreateElement("srcdir")
            childelement1.InnerText =  <value>
            childelement.AppendChild(childelement1)

            xmldoc.DocumentElement.AppendChild(childelement)

            xmldoc.Save(<file name>)

The above code will append a a node named app and include two elements named <name> and <srcdir> and assign the value for that element(va;ue of the inertext) and save it in the specified xml file

Thanks,
Nanda
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now