Solved

Add to XML File

Posted on 2006-06-16
3
263 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
[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
  • 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

726 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