Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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

Reply With Quote
  #2 (permalink)  
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 -/
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
  #4 (permalink)  
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

Reply With Quote
  #5 (permalink)  
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 -/
Reply With Quote
  #6 (permalink)  
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
Reply With Quote
  #7 (permalink)  
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
Reply With Quote
  #8 (permalink)  
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

Reply With Quote
  #9 (permalink)  
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!


Reply With Quote
  #10 (permalink)  
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:25 AM.


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