Solved

Appending an XML file

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now