p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XML
I forgot my password Register Now
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 26th, 2008, 11: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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old March 26th, 2008, 11:31 AM
samjudson's Avatar
Friend of Wrox
Points: 4,453, Level: 28
Points: 4,453, Level: 28 Points: 4,453, Level: 28 Points: 4,453, Level: 28
Activity: 60%
Activity: 60% Activity: 60% Activity: 60%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 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 -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old March 26th, 2008, 11:34 AM
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old March 26th, 2008, 11: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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old March 26th, 2008, 11:54 AM
samjudson's Avatar
Friend of Wrox
Points: 4,453, Level: 28
Points: 4,453, Level: 28 Points: 4,453, Level: 28 Points: 4,453, Level: 28
Activity: 60%
Activity: 60% Activity: 60% Activity: 60%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 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 -/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old March 26th, 2008, 12:10 PM
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old March 26th, 2008, 01:07 PM
mhkay's Avatar
Wrox Author
Points: 12,735, Level: 48
Points: 12,735, Level: 48 Points: 12,735, Level: 48 Points: 12,735, Level: 48
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old March 27th, 2008, 08: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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #9 (permalink)  
Old April 17th, 2008, 04: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!


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #10 (permalink)  
Old April 17th, 2008, 04:39 PM
joefawcett's Avatar
Wrox Author
Points: 8,994, Level: 40
Points: 8,994, Level: 40 Points: 8,994, Level: 40 Points: 8,994, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 2,922
Thanks: 0
Thanked 13 Times in 12 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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 07:59 AM
creating new xml file from existing deepee XML 1 July 25th, 2006 11:35 AM
inserting XML/ASCII data into SQL using VB.net outcast1881 Other Programming Languages 0 July 20th, 2006 08: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 01:07 PM



All times are GMT -4. The time now is 07:02 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc