Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
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 Display Modes
  #1 (permalink)  
Old August 3rd, 2009, 07:35 AM
Authorized User
 
Join Date: Aug 2009
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
Default Adding a new Node to existing XML document

Hello everyone!

I'm newbie around here, but i heard that this is a right place for me.
I'm writing a storeyard program with multiple database (XML,MDF,XLS...) in VB.NET.
I have a problem with adding a new node (child) in XML file.
Here is code:


Code:
Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
Dim writer As New XmlTextWriter("C:\Documents and Settings\Vladimir\My Documents\Visual Studio 2008\Projects\skladiste_app\sklad.xml", System.Text.Encoding.UTF8)
writer.WriteStartDocument(True)
writer.Formatting = Formatting.Indented
writer.Indentation = 2
writer.WriteStartElement("article")
create_node(sybling_count.ToString, name_article_XML.Text, type_article_xml.Text, Cypher_article_xml.Text, num_xml.Text, writer)
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()
End Sub

Private Sub create_node(ByVal pID As String, ByVal pName As String, ByVal pType As String, ByVal pCyph As String, ByVal pNum As String, ByVal writer As XmlTextWriter)

writer.WriteStartElement("artikal")
writer.WriteStartElement("ID_article")
writer.WriteString(pID)
writer.WriteEndElement()
writer.WriteStartElement("Name")
writer.WriteString(pName)
writer.WriteEndElement()
writer.WriteStartElement("Type")
writer.WriteString(pType)
writer.WriteEndElement()
writer.WriteStartElement("Cyphre")
writer.WriteString(pSyph)
writer.WriteEndElement()
writer.WriteStartElement("Number")
writer.WriteString(pNum)
writer.WriteEndElement()
writer.WriteEndElement()
End Sub
The issue is that on every data writing it overwrites the old data.
I know there must be an easy way for adding new child to the end of the document. I found some tags for xml but this suppose to be an automatically generated file, so it doesn't work for me.
Those variable names are changed, originally they were in Croatian language, so if you find some irregularity with program flow, ignore it.

Any help is appreciated!
Reply With Quote
  #2 (permalink)  
Old August 3rd, 2009, 07:43 AM
Friend of Wrox
Points: 6,414, Level: 34
Points: 6,414, Level: 34 Points: 6,414, Level: 34 Points: 6,414, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,192
Thanks: 0
Thanked 235 Times in 234 Posts
Default

With the .NET framework you have a couple of options. Since .NET 3.5 you can use LINQ to XML (System.Xml.Linq.XDocument), with earlier versions you can use the DOM implementation (System.Xml.XmlDocument) in the .NET framework. See http://msdn.microsoft.com/en-us/library/bb387098.aspx for an introduction to LINQ to XML and http://msdn.microsoft.com/en-us/library/t058x2df.aspx for using the DOM implementation.
With XmlDocument code looks like
Code:
Dim doc As New XmlDocument()
doc.Load("file.xml")
Dim foo As XmlElement = doc.CreateElement("foo")
Dim bar As XmlElement = doc.CreateElement("bar")
bar.InnerText = "whatever"
foo.AppendChild(bar)
doc.DocumentElement.AppendChild(foo)
doc.Save("file.xml")
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
Lupus81 (August 10th, 2009)
  #3 (permalink)  
Old August 3rd, 2009, 10:07 AM
Authorized User
 
Join Date: Aug 2009
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
Default

OK, i'll try that.
Thanx!
Reply With Quote
  #4 (permalink)  
Old August 4th, 2009, 04:57 PM
Authorized User
 
Join Date: Aug 2009
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Martin!

Your solution works. Thanks!
Reply With Quote
Reply


Thread Tools
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
Problem adding a xml node Lupus81 Visual Basic 2008 Professionals 1 August 4th, 2009 04:56 PM
adding new data into existing fields? tangwt Access 1 March 30th, 2009 08:10 AM
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
How to insert a New node into the XML Document VB jabrouni1 Classic ASP XML 1 January 26th, 2007 05:11 AM
Adding existing and non-existing attributes spencer.clark XSLT 5 July 27th, 2005 04:02 PM



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


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