|
-
Jun 2nd, 2006, 07:31 AM
#1
Thread Starter
Fanatic Member
[2005] Adding data to an already existing XML file?
I am having some trouble working with XML files. I have some code that checks if the XML file exists and if not, creates it. If it does exist, it simply loads it. That works fine. Here's the code for that
If Not System.IO.File.Exists("C:\temp\test.xml") Then
'XMLDoc = New XmlDocument()
Dim XMLDeclaration As XmlDeclaration = XMLDoc.CreateXmlDeclaration("1.0", "utf-8", "")
Dim XMLRootNode As XmlElement = XMLDoc.CreateElement("CDLIST")
XMLDoc.InsertBefore(XMLDeclaration, XMLDoc.DocumentElement)
XMLDoc.AppendChild(XMLRootNode)
XMLDoc.Save("C:\temp\test.xml")
Else
XMLDoc.Load("C:\temp\test.xml")
End If
Now, what I want it do is add a section that looks like this
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
This code sort of works but doesn't really What am I doing wrong?
Dim ParentNode As XmlElement = XMLDoc.DocumentElement.PrependChild(XMLDoc.CreateElement("TEST"))
ParentNode.AppendChild(XMLDoc.CreateElement("Owner"))
ParentNode.AppendChild(XMLDoc.CreateTextNode("asdasd"))
ParentNode.AppendChild(XMLDoc.CreateElement("type"))
ParentNode.AppendChild(XMLDoc.CreateTextNode("123213"))
ParentNode.AppendChild(XMLDoc.CreateElement("test"))
ParentNode.AppendChild(XMLDoc.CreateTextNode("kouewo"))
-
Jun 2nd, 2006, 07:36 AM
#2
Thread Starter
Fanatic Member
Re: [2005] Adding data to an already existing XML file?
Ah OK, this works but it seems a little "clunky", is there a better way or is this correct?
Dim ParentNode As XmlElement = XMLDoc.DocumentElement.PrependChild(XMLDoc.CreateElement("TEST"))
Dim OwnerElement As XmlElement = ParentNode.AppendChild(XMLDoc.CreateElement("Owner"))
OwnerElement.AppendChild(XMLDoc.CreateTextNode("asdasd"))
-
Jun 6th, 2006, 07:28 AM
#3
Thread Starter
Fanatic Member
Re: [2005] Adding data to an already existing XML file?
-
Jun 6th, 2006, 07:32 AM
#4
Frenzied Member
Re: [2005] Adding data to an already existing XML file?
 Originally Posted by Ginolard
Anyone?
well u could always extract all the data and rebuild the xml...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|