Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
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 March 26th, 2008, 10:24 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default VB.net, adding XML data to an existing XML file

Hello all,

I have a problem with VB.net and Xml

I have an xml file with about this lay-out.

<visitor>
 <name>blabla</name>
 <lastname>blabla</lastname>
.....
</visitor>

Now I have a form with some textboxes and when I click a button those textboxes are written to the XML file.

Problem : the new values overwrite the old values.
I want to add a new visitor and keep the old one. Like this:

<visitor>
 <name>blabla1</name>
 <lastname>blabla1</lastname>
.....
</visitor>
<visitor>
 <name>blabla2</name>
 <lastname>blabla2</lastname>
.....
</visitor>

Is that possible?
thx in advance

 
Old March 26th, 2008, 10:31 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Sure it is. But it would be much easier to tell you how to do it if you showed us the code that isn't working.

/- Sam Judson : Wrox Technical Editor -/
 
Old March 26th, 2008, 10:34 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

It is certainly possible, one way to achieve that is by using System.Xml.XmlDocument and load your XML document once, then work with the in-memory DOM document to add nodes, then later save the DOM document back to file:

Dim doc As New XmlDocument()
doc.Load("doc.xml")

Now store that XmlDocument instance in a member variable and write a method to add a visitor e.g.

Sub AddVisitor (ByVal name As String, ByVal lastName As String)
  Dim visitor As XmlElement = doc.CreateElement("visitor")
  Dim nameEl As XmlElement = doc.CreateElement("name")
  nameEl.InnerText = name
  Dim lastNameEl As XmlElement = doc.CreateElement("lastname")
  lastNameEl.InnerText = lastName
  visitor.AppendChild(nameEl)
  visitor.AppendChild(lastNameEl)
  doc.DocumentElement.AppendChild(visitor)
End Sub

Then save changes later with e.g.
  doc.Save("doc.xml")

Also note to use XmlDocument you need to have a root elements (e.g. "visitors") for your "visitor" elements.

Microsoft MVP - XML
 
Old March 26th, 2008, 10:44 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I will try this but wont the program use a lot of resourses when the XML file grows bigger and bigger?

thanks already for the quick reply

 
Old March 26th, 2008, 10:54 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you are using this as a log file which is likely to grow into the 10's or 100's of MB then yes, this is a very bad way of doing it as you need to load the entire file into memory each time you want to add something to it.

That is why most log files are just a glorified CSV file format.

If you really must use XML then a work around is to create a visitors.xml file which looks something like this:

Code:
<!DOCTYPE staticinc [ <!ENTITY logentries SYSTEM "visitors_log.xml"> ]> 
<root> 
&logentries; 
</root>
And then the visitors_log.xml file can be treated as a simple text file and appended to quickly with your XML above.

Code:
StreamWriter sw = File.AppendText("visitors_log.xml");
sw.Write("<visitor>");
sw.Write("<name>" + name + "</name>");
sw.Write("<lastname>" + lastName + "</lastname>");
sw.WriteLine("</visitor>");
sw.Close();
Then if you need to process the log as a XML file you can call XmlDocument.Load on the visitors.xml file.

/- Sam Judson : Wrox Technical Editor -/
 
Old March 26th, 2008, 11:10 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can load the XML document once and keep it in memory from there one, only saving when your application is closed down or sometimes in between for safety.
Of course such a DOM document consumes memory and consumes more memory when the document grows but so would a DataSet or DataTable.
If you want to save changes only in an incremental way then use a data base system, an XML document is not the right choice in that case.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old March 26th, 2008, 12:07 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Another technique for appending data to an XML file is to maintain a file in the form

<record>...</record>
<record>...</record>
<record>...</record>
<record>...</record>

and then simply append a new record using file level I/O. The problem of course is that the above isn't a well-formed XML document because it has no wrapper element. But you can get around that by importing it as an entity into a document that supplies the wrapper:

<!DOCTYPE visitors [
<!ENTITY e SYSTEM "logdata.xml">
]>
<visitors >&e;</visitors>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 27th, 2008, 07:43 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried the method of Martin Honnen and it works fine.
I will have to watch the resources closely. But as long as it is no realy big file i hope to be save.

thx alot everybody

 
Old April 17th, 2008, 03:25 PM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have a quick question on this. When adding the new visitor, is there a way to append to the top of the file so that it's like this:

<visitor>
 <name>blabla2</name>
 <lastname>blabla2</lastname>
</visitor>

<visitor>
 <name>blabla1</name>
 <lastname>blabla1</lastname>
</visitor>

I was able to make mine work via VB.net and had to change some of what was here but used it as a stepping stone to get it working. However, since it's "New Information" that's posted online, I'd like it to be posted at the top? Is this possible or is appending always towards the bottom?

Thanks!


 
Old April 17th, 2008, 03:39 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Look at http://msdn2.microsoft.com/en-us/lib...ertbefore.aspx for the InsertBefore method. You can insert before the first child element of the document element, doc.DocumentElement.FirstChild.

--

Joe (Microsoft MVP - XML)




Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple input xml / get data from other xml file elayaraja.s XSLT 3 July 25th, 2008 06:59 AM
creating new xml file from existing deepee XML 1 July 25th, 2006 10:35 AM
inserting XML/ASCII data into SQL using VB.net outcast1881 Other Programming Languages 0 July 20th, 2006 07:39 AM
Getting VB.NET to load & update XML file kyma XML 0 March 19th, 2005 04:58 PM
XML to XML through an XSLT (VB.NET) dimondwoof XSLT 1 June 25th, 2003 12:07 PM





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