Solved

Appending an XML file

Posted on 2006-07-07
2
247 Views
Last Modified: 2010-05-18
I am using Visual Basic .NET. Here is my sample XML file:

<?xml version="1.0" encoding="utf-8" ?>
<content>
      <header>
            <title>This is a test</title>
      </header>
      <item>
            <title>Item title test</title>
            <description>Here is a description of an item</description>
            <name>here is a file name</name>
      </item>
      <item>
            <title>Item 2 title</title>
            <description>Here is a description of 2</description>
            <name>here is 2's file name</name>
      </item>
</content>

I am parse the file fine but now I want to append it so I can add a new 'item' to the file. I am fairly new to XML and Visual Basic .NET. So how would I add a new item with title="Hello World", description="This is a test" and name="VB"? And finally save the XML file. Code would help.

Thank you for your help.
0
Comment
Question by:Linky
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 125 total points
ID: 17061630
You could to it with something like:

        Dim doc As New XmlDocument()
        doc.Load(xmlPath)

        Dim newItem As XmlNode
        Dim newNode As XmlNode

        newItem = doc.CreateElement("item")

        newNode = doc.CreateElement("title")
        newNode.InnerText = "Hello World"
        newItem.AppendChild(newNode)

        newNode = doc.CreateElement("description")
        newNode.InnerText = "This is a test"
        newItem.AppendChild(newNode)

        newNode = doc.CreateElement("name")
        newNode.InnerText = "VB"
        newItem.AppendChild(newNode)

        doc.DocumentElement.AppendChild(newItem)
        doc.Save(xmlPath)
0
 
LVL 2

Author Comment

by:Linky
ID: 17061678
Wow that worked well. Thank you very much.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Get hold of longitude and latitude in iframe string 11 48
Variable Event ? 3 36
Write to file when application closes (by logoff) - VB.NET 1 37
Disable extension 8 48
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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