Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 5th, 2010, 07:38 PM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Unhappy How do I add to an XML file using VB?

I've been coding in VBA, using Excel to store information. That's fine for my job, and for most home usage. But now I'm wanting to create more flexible, more portable applications. That is why I'm turning to XML.

What I need to do is to add new items to an XML file. Everything I've tried merely replaces the xml file ... it doesn't add the new data to the end of the "list," which is what I want it to do.

My most recent attempt in VB used this code:

Code:
Imports System.Xml
Imports System.Xml.Linq

Public Class Form1

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim xmlDoc As XDocument = XDocument.Load("XMLFile.xml")

        Dim persons = From person In xmlDoc.Descendants("Person") _
        Select Name = person.Element("Name").Value, City = person.Element("City").Value, _
        Age = person.Element("Age").Value

        RichTextBox1.Text = ""
        For Each person In persons
            RichTextBox1.Text = RichTextBox1.Text & "Name: " & person.Name + Constants.vbLf
            RichTextBox1.Text = RichTextBox1.Text & "City: " & person.City + Constants.vbLf
            RichTextBox1.Text = RichTextBox1.Text & "Age: " & person.Age + Constants.vbLf + Constants.vbLf
        Next person

        If RichTextBox1.Text = "" Then
            RichTextBox1.Text = "No Results."
        End If
    End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xml As New XElement("Persons", _
                    New XElement("Person", _
                    New XElement("Name", txtName.Text), _
                    New XElement("City", txtCity.Text), _
                    New XElement("Age", txtAge.Text)))

        xml.Save("XMLFile.xml")
    End Sub
End Class

The xml file that this code accesses looks like this:

Code:
<?xml version="1.0" encoding="utf-8"?>
<Persons>
  <Person>
    <Name>Name</Name>
    <City>City</City>
    <Age>Age</Age>
  </Person>
</Persons>
The example I was toying with came from this url: http://www.dotnettutorials.com/tutor...ml-add-vb.aspx


Just like everything else I've tried, this code merely replaces the xml file (contrary to what the tutorial seems to indicate). It does not preserve the old data and add the new data.

I seriously need help!
 
Old April 6th, 2010, 05:16 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm afraid I don't use LinQ XML much myself, but as you can probably tell when you call New XElement("Persons") you are creating a brand new XML document which you are then just overwriting over the old document.

Something like this would need to be done:

Code:
Dim xml As New XElement("Person", _
                    New XElement("Name", txtName.Text), _
                    New XElement("City", txtCity.Text), _
                    New XElement("Age", txtAge.Text)))

Dim xmlDoc As XDocument = XDocument.Load("XMLFile.xml")

xmlDoc.Element("Persons").Add(xml)

xmlDoc.Save("XMLFile.xml")
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
QuadFather (April 6th, 2010)
 
Old April 6th, 2010, 12:28 PM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Default Works great! (w/ one little edit)

You are my favorite person of the moment!

Here is the code that worked:

Code:
Dim exml As New XElement("Person", _
                    New XElement("Name", txtName.Text), _
                    New XElement("City", txtCity.Text), _
                    New XElement("Age", txtAge.Text))

        Dim xmlDoc As XDocument = XDocument.Load("XMLFile.xml")

        xmlDoc.Element("Persons").Add(exml)

        xmlDoc.Save("XMLFile.xml")
The changes I made were:
  • Deleted an extra parenthesis after the last "New XElement", since there is no longer a new "Persons"
  • Replaced "xml" with "exml", since ".Add(xml)" is apparently illegal use of "xml"

Again, I can't thank you enough. You've just opened up the world of xml for me.
 
Old April 6th, 2010, 12:36 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Note that with VB.NET and LINQ to XML you could even use VB.NET XML literals in your code e.g.
Code:
xmlDoc.Element("Persons").Add(<Person>
  <Name><%= txtName.Text %></Name>
  <City><%= txtCity.Text %></City>
  <Age><%= txtAge.Text %></Age>
</Person>)
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
QuadFather (April 6th, 2010)
 
Old April 6th, 2010, 02:32 PM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Thumbs up I'm getting smarter and smarter!

Wow, another great solution. I'll probably use this one, since I can see the XML plain as day ... funny how i couldn't even find a single solution by searching the internet.

You guys are fantastic. Wish I could give you a thousand thanks, but the thanks button only registers one click.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add nodes to an existing xml file using vb.net ammu86 XML 11 January 19th, 2010 09:13 AM
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
Create XML file - add elements according to range remya1000 XML 5 October 3rd, 2007 02:50 PM
Create XML file - According to the Value, add elem remya1000 General .NET 0 October 2nd, 2007 11:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.