Solved

Appending an XML file

Posted on 2006-07-07
2
248 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 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 46
SSRS 2016 Rendering HTML tables 3 56
VB.net Filesystem watcher not working 5 34
VB.NET System.NullReferenceException 1 18
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

763 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