Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
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 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!
 
Old August 3rd, 2009, 07:43 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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
The Following User Says Thank You to Martin Honnen For This Useful Post:
Lupus81 (August 10th, 2009)
 
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!
 
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!





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 06:11 AM
Adding existing and non-existing attributes spencer.clark XSLT 5 July 27th, 2005 04:02 PM





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