Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Appending an XML file

Posted on 2006-07-07
2
Medium Priority
?
256 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 500 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

885 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