Results 1 to 10 of 10

Thread: [RESOLVED] [2005]Updating XML in VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    9

    Resolved [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
    Attached Files Attached Files

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    9

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    9

    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
    Last edited by Rustyfuture; Apr 12th, 2008 at 05:47 AM.

  5. #5
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    9

    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

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    9

    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.

  9. #9
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005]Updating XML in VB

    vb.net Code:
    1. Dim doc As New XmlDocument()
    2. doc.Load("c:\dvd.xml")
    3.  
    4. doc.GetElementsByTagName("DVDInfo")
    5. Dim e0 As XmlElement = doc.CreateElement("DVD")
    6.  
    7. Dim e1 As XmlElement = doc.CreateElement("DVDTitle")
    8. Dim e1t As XmlText = doc.CreateTextNode("Goodfellas")
    9. Dim e2 As XmlElement = doc.CreateElement("Rating")
    10. Dim e2t As XmlText = doc.CreateTextNode("Awesome")
    11.  
    12. e0.AppendChild(e1)
    13. e0.LastChild.AppendChild(e1t)
    14. e0.AppendChild(e2)
    15. e0.LastChild.AppendChild(e2t)
    16.  
    17. doc.DocumentElement.AppendChild(e0)
    18.  
    19. 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:
    1. Dim r As String = Environment.NewLine
    2. Dim newDVD As String = _
    3.     "<DVD>" & r & _
    4.     "   <DVDTitle>Ghostbusters</DVDTitle>" & r & _
    5.     "   <Rating>Awesome</Rating>" & r & _
    6.     "</DVD>"
    7.  
    8. Dim doc As New XmlDocument()
    9. doc.Load("c:\dvd.xml")
    10.  
    11. Dim frag As XmlDocumentFragment = doc.CreateDocumentFragment()
    12. frag.InnerXml = newDVD
    13.  
    14. Dim root As XmlNode = doc.DocumentElement
    15. root.AppendChild(frag)
    16.  
    17. doc.Save("c:\dvd.xml")

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    9

    Thumbs up Re: SOLVED[2005]Updating XML in VB

    Thank you for all your help guys, I really appriciate it.

    Thanks again,

    Rusty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width