Solved

Add to XML File

Posted on 2006-06-16
3
261 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

830 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