1 Attachment(s)
[RESOLVED] [2005]Updating XML in VB
Hi Guys,
I have a friend who is developing a dvd store in Visual basic ( It's a WPF application) and stores the data in an XML file
The XML file is attached.
He needs a way to update the file so he can add other DVD's into the file from a form in vb
He has a Textbox input for each node from DVD title through to Image and a button on the form to update the file when it is clicked.
Can someone help?
thanks guys,
Rusty
Re: [2005]Updating XML in VB
To read, edit and save an XML file you would use an XmlDocument object. You'd call the Load method to load a file and Save to save the modified data back to the file. In between you'd do whatever was necessary to edit the data as you required, e.g. call the AddNode method to add a node.
I'd suggest that you have a look at this first, then follow some of the links from here. If you still have trouble then post back, show us what you've done and tell us what you're having trouble with.
Re: [2005]Updating XML in VB
thanks for the info, jmcilhinney. I shall have a look now.
Just a quick question what is the difference between an XML element and XML node?
thanks,
Rusty
Re: [2005]Updating XML in VB
Hi jmcilhinney,
thats what i've got so far
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim doc As New XmlDocument
doc.Load("C:\Documents and Settings\p068612\Desktop\DVD.xml")
doc.GetElementsByTagName("DVDInfo")
doc.CreateElement("DVDTitle")
doc.InnerText = Me.txttitle.Text
doc.CreateElement("Director")
doc.InnerText = Me.txtdirector.Text
doc.CreateElement("Cast")
doc.InnerText = Me.txtcast.Text
doc.CreateElement("Description")
doc.InnerText = Me.txtdescription.Text
doc.CreateElement("Rating")
doc.InnerText = Me.txtrating.Text
doc.CreateElement("Cost")
doc.InnerText = Me.txtcost.Text
doc.CreateElement("Copies")
doc.InnerText = Me.txtcopies.Text
doc.CreateElement("Rental")
doc.InnerText = Me.txtRental.Text
doc.CreateElement("Image")
doc.InnerText = Me.Txtimage.Text
doc.Save("C:\Documents and Settings\p068612\Desktop\DVD.xml")
WhenI try an run it I get the following error.
"Object reference not set to an instance of an object" on the line
Code:
doc.InnerText = Me.txttitle.Text
I need a bit of help with the logic between the lines of code.
I have a Textbox on the form for each attribute of a DVD, could you give me an example to get me on my way please?
Say I have a dvd title textbox and I want to write the contents of the textbox to a new dvd tilte node in the XML file.
so I want this basically
<DVD>
<DVDTitle> texbox.text</DVDtitle>
</DVD>
I also need it to be withn the <DVDInfo> </DVDInfo> tags within the file.
Thanks
Rusty
Re: [2005]Updating XML in VB
Code:
Dim doc As New XmlDocument
doc.Load("C:\Documents and Settings\p068612\Desktop\DVD.xml")
doc.GetElementsByTagName("DVDInfo")
doc.CreateElement("DVDTitle")
doc.InnerText = Me.txttitle.Text
doc is your XML document. You want to set your InnerText property of your element, not your entire XML document.
Here are the examples you are looking for:
http://msdn2.microsoft.com/en-us/library/fw1ys7w6.aspx
http://msdn2.microsoft.com/en-us/lib...innertext.aspx
This may perhaps be helpful as well:
http://www.vbforums.com/showthread.php?t=478692
Re: [2005]Updating XML in VB
Hi nmadd,
I have altered the code to the following:
Code:
Dim doc As New XmlDocument
Dim elem1 As XmlElement = doc.CreateElement("DVDTitle")
Dim elem2 As XmlElement = doc.CreateElement("Director")
Dim elem3 As XmlElement = doc.CreateElement("Cast")
Dim elem4 As XmlElement = doc.CreateElement("Description")
Dim elem5 As XmlElement = doc.CreateElement("Rating")
Dim elem6 As XmlElement = doc.CreateElement("Cost")
Dim elem7 As XmlElement = doc.CreateElement("Copies")
Dim elem8 As XmlElement = doc.CreateElement("Rental")
Dim elem9 As XmlElement = doc.CreateElement("image")
Dim text1 As XmlText = doc.CreateTextNode(Me.txttitle.Text)
Dim text2 As XmlText = doc.CreateTextNode(Me.txtdirector.Text)
Dim text3 As XmlText = doc.CreateTextNode(Me.txtcast.Text)
Dim text4 As XmlText = doc.CreateTextNode(Me.txtdescription.Text)
Dim text5 As XmlText = doc.CreateTextNode(Me.txtrating.Text)
Dim text6 As XmlText = doc.CreateTextNode(Me.txtcost.Text)
Dim text7 As XmlText = doc.CreateTextNode(Me.txtcopies.Text)
Dim text8 As XmlText = doc.CreateTextNode(Me.txtRental.Text)
Dim text9 As XmlText = doc.CreateTextNode(Me.Txtimage.Text)
doc.Load("DVD.xml")
doc.GetElementsByTagName("DVDInfo")
doc.DocumentElement.AppendChild(elem1)
doc.DocumentElement.LastChild.AppendChild(text1)
doc.DocumentElement.AppendChild(elem2)
doc.DocumentElement.LastChild.AppendChild(text2)
doc.DocumentElement.AppendChild(elem3)
doc.DocumentElement.LastChild.AppendChild(text3)
doc.DocumentElement.AppendChild(elem4)
doc.DocumentElement.LastChild.AppendChild(text4)
doc.DocumentElement.AppendChild(elem5)
doc.DocumentElement.LastChild.AppendChild(text5)
doc.DocumentElement.AppendChild(elem6)
doc.DocumentElement.LastChild.AppendChild(text6)
doc.DocumentElement.AppendChild(elem7)
doc.DocumentElement.LastChild.AppendChild(text7)
doc.DocumentElement.AppendChild(elem8)
doc.DocumentElement.LastChild.AppendChild(text8)
doc.DocumentElement.AppendChild(elem9)
doc.DocumentElement.LastChild.AppendChild(text9)
doc.Save("DVD.xml")
The XML file looks like the following
Code:
<DVDInfo>
<DVD>
<DVDTitle>The Exorcist</DVDTitle>
<Director>William Friedkin</Director>
<Cast>Linda Blair</Cast>
<Description>Horror</Description>
<Rating>18</Rating>
<Cost>7.99</Cost>
<Copies>0</Copies>
<Rental>0</Rental>
<Image>Exorcist.jpeg</Image>
</DVD>
<DVDTitle>test2</DVDTitle>
<Director>test2</Director>
<Cast>test2</Cast>
<Description>test2</Description>
<Rating>test2</Rating>
<Cost>test2</Cost>
<Copies>test2</Copies>
<Rental>test2</Rental>
<image>test2</image>
</DVDInfo>
I'm missing the top <DVD> tag on the last dvd and the closing </DVD> just befor the closing </DVDInfo> tag.
Any help with putting these in will be greatly appriciated,
Thanks rusty
Re: [2005]Updating XML in VB
Because you are getting
doc.GetElementsByTagName("DVDInfo")
Which looks for <DVDInfo>
It then starts adding your nodes to the DVDInfo node. But you have specified no DVD element. Create an XML node called <DVD>, and then append all your elemx and textx to that node, then add that node to your DVDInfo.
Re: [2005]Updating XML in VB
Quote:
Originally Posted by mendhak
Because you are getting
doc.GetElementsByTagName("DVDInfo")
Which looks for <DVDInfo>
It then starts adding your nodes to the DVDInfo node. But you have specified no DVD element. Create an XML node called <DVD>, and then append all your elemx and textx to that node, then add that node to your DVDInfo.
Code:
Dim elem0 As XmlElement = doc.CreateElement("DVD")
I have created the <DVD> Node
But I don't understand where to put it in my code.
Re: [2005]Updating XML in VB
vb.net Code:
Dim doc As New XmlDocument()
doc.Load("c:\dvd.xml")
doc.GetElementsByTagName("DVDInfo")
Dim e0 As XmlElement = doc.CreateElement("DVD")
Dim e1 As XmlElement = doc.CreateElement("DVDTitle")
Dim e1t As XmlText = doc.CreateTextNode("Goodfellas")
Dim e2 As XmlElement = doc.CreateElement("Rating")
Dim e2t As XmlText = doc.CreateTextNode("Awesome")
e0.AppendChild(e1)
e0.LastChild.AppendChild(e1t)
e0.AppendChild(e2)
e0.LastChild.AppendChild(e2t)
doc.DocumentElement.AppendChild(e0)
doc.Save("c:\dvd.xml")
Code:
<?xml version="1.0" encoding="windows-1250"?>
<DVDInfo>
<DVD>
<DVDTitle>Goodfellas</DVDTitle>
<Rating>Awesome</Rating>
</DVD>
</DVDInfo>
Also consider trying the XmlDocumentFragment approach. I find this considerably easier:
vb.net Code:
Dim r As String = Environment.NewLine
Dim newDVD As String = _
"<DVD>" & r & _
" <DVDTitle>Ghostbusters</DVDTitle>" & r & _
" <Rating>Awesome</Rating>" & r & _
"</DVD>"
Dim doc As New XmlDocument()
doc.Load("c:\dvd.xml")
Dim frag As XmlDocumentFragment = doc.CreateDocumentFragment()
frag.InnerXml = newDVD
Dim root As XmlNode = doc.DocumentElement
root.AppendChild(frag)
doc.Save("c:\dvd.xml")
Re: SOLVED[2005]Updating XML in VB
Thank you for all your help guys, I really appriciate it.
Thanks again,
Rusty