|
|
 |
| 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.
|
 |
|

March 26th, 2008, 11:24 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

March 26th, 2008, 11:31 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 Posts
|
|
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 -/
|

March 26th, 2008, 11:34 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
|
|
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
|

March 26th, 2008, 11:44 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

March 26th, 2008, 11:54 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 1,359
Thanks: 0
Thanked 31 Times in 31 Posts
|
|
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 -/
|

March 26th, 2008, 12:10 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
|
|
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
|

March 26th, 2008, 01:07 PM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
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
|

March 27th, 2008, 08:43 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

April 17th, 2008, 04:25 PM
|
|
Registered User
|
|
Join Date: Apr 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|

April 17th, 2008, 04:39 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 2,922
Thanks: 0
Thanked 13 Times in 12 Posts
|
|
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)
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |