Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Appending an XML file

Posted on 2006-07-07
2
Medium Priority
?
251 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
[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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

730 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