8 Replies - 1324 Views - Last Post: 06 September 2012 - 01:47 PM Rate Topic: -----

#1 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 711
  • Joined: 09-May 09

LINQ to XML update element value

Posted 04 September 2012 - 02:31 PM

Hi guys,

Im trying to update a value in an xml file. Ive had no success so far. Im trying to follow the MSDN guide but im unable to update the value.

Sub save2(ByVal xmlFilePath As String)

        Try
            'load document
            Dim movieData = Xdocument.Load(xmlFilePath)

            'get position and set value
            For Each movie In From element In movieData.<movies>.<movie>
                movie.<name>.Value = "TEST VALUE"
            Next

        Catch ex As Exception

        End Try
    End Sub



my xml data

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <opensearch:Query searchTerms="44833"/>
  <opensearch:totalResults>1</opensearch:totalResults>
  <movies>
    <movie>
      <popularity>211195.458</popularity>
      <translated>true</translated>
      <adult>false</adult>
      <language>en</language>
      <original_name>Battleship</original_name>
      <name>Battleship</name>
      <alternative_name>Battleship, el desafío</alternative_name>
      <type>movie</type>
      <id>44833</id>
      <imdb_id>tt1440129</imdb_id>



Before i resorted to creating a new xml file but as my project has grown this is not adaquate

any help would be great.
kevin

Is This A Good Question/Topic? 0
  • +

Replies To: LINQ to XML update element value

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 230
  • View blog
  • Posts: 753
  • Joined: 19-October 11

Re: LINQ to XML update element value

Posted 05 September 2012 - 07:29 AM

Have you found the solution yet? If not, and don't want to dig further yourself, take a look:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 711
  • Joined: 09-May 09

Re: LINQ to XML update element value

Posted 05 September 2012 - 02:16 PM

Thanks for the reply,

i had tried this oreviously (i use it to query) but it does not work

 Try
            'load document
            Dim movieData = Xdocument.Load(xmlFilePath)

            'get position and set value
            For Each movie In From element In movieData.Descendants("movie")

                movie.<name>.Value = "Test Value"

            Next


        Catch ex As Exception

        End Try



any other ideas?

This post has been edited by deery5000: 05 September 2012 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1998
  • View blog
  • Posts: 8,808
  • Joined: 29-May 08

Re: LINQ to XML update element value

Posted 05 September 2012 - 02:16 PM

A useful feature of the vb.net IDE, is if you include a XML Schema you'll get Intellisense support for the xml the schema covers. Which is cool! and useful.

' I just pasted the example xml into the vb.net code '
        Dim movieXML = <?xml version="1.0" encoding="UTF-8"?>
                       <OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
                           <opensearch:Query searchTerms="44833"/>
                           <opensearch:totalResults>1</opensearch:totalResults>
                           <movies>
                               <movie>
                                   <popularity>211195.458</popularity>
                                   <translated>true</translated>
                                   <adult>false</adult>
                                   <language>en</language>
                                   <original_name>Battleship</original_name>
                                   <name>Battleship</name>
                                   <alternative_name>Battleship, el desafío</alternative_name>
                                   <type>movie</type>
                                   <id>44833</id>
                                   <imdb_id>tt1440129</imdb_id>
                               </movie>
                           </movies>
                       </OpenSearchDescription>
' You should see the extra node.'
        For Each m In movieXML.<OpenSearchDescription>.<movies>.<movie>
            m.<name>.Value = "Test Data"
        Next


or you code use the ... which is any matching node no matter how deep.
For Each m In movieXML...<movie>
  m.<name>.Value = "Test Data"
Next


This post has been edited by AdamSpeight2008: 05 September 2012 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#5 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 711
  • Joined: 09-May 09

Re: LINQ to XML update element value

Posted 05 September 2012 - 03:16 PM

Ive tried both metheds you outlined Adam, the code executes but the value is not updated. Is there something im missing here.

Can a value be updated in an .xml file?

kevin
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1998
  • View blog
  • Posts: 8,808
  • Joined: 29-May 08

Re: LINQ to XML update element value

Posted 05 September 2012 - 03:23 PM

It does update. And you still need to save the xml to a file.
Was This Post Helpful? 0
  • +
  • -

#7 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 711
  • Joined: 09-May 09

Re: LINQ to XML update element value

Posted 05 September 2012 - 03:33 PM

ah i see, ill try this and get back to you.
Was This Post Helpful? 0
  • +
  • -

#8 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 230
  • View blog
  • Posts: 753
  • Joined: 19-October 11

Re: LINQ to XML update element value

Posted 05 September 2012 - 10:09 PM

deery5000 as Adam pointed out, you were missing the extra <OpenSearchDescription> node. You said that For Each movie In From element In movieData.Descendants("movie") doesn't work, but it does updates, not saves. It is the same as movieXML...<movie>.
Was This Post Helpful? 0
  • +
  • -

#9 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 711
  • Joined: 09-May 09

Re: LINQ to XML update element value

Posted 06 September 2012 - 01:47 PM

Solved . . .

Thanks a million guys

 Try
            'load document
            Dim movieData = Xdocument.Load(xmlFilePath)

            'movie data
            For Each m In movieData.<OpenSearchDescription>.<movies>.<movie>

                m.<name>.Value = Form1.movieNameTextBox.Text
                m.<tagline>.Value = Form1.tagLineTextBox.Text
                m.<rating>.Value = Form1.RatingStar1.RatingValue * 2
                m.<overview>.Value = Form1.movieDescriptionTextBox.Text
                m.<name>.Value = Form1.trailerLinkLabel.Text
                m.<trailer>.Value = Form1.trailerLinkLabel.Text
                m.<url>.Value = Form1.tmdbLinkLabel.Text
                m.<homepage>.Value = Form1.tmdbLinkLabel.Text
                m.<name>.Value = Form1.runtimeTextBox.Text
                m.<released>.Value = Form1.releasedTextBox.Text
                m.<certification>.Value = Form1.certificationTextBox.Text
                m.<budget>.Value = Form1.budgetTextBox.Text
                m.<revenue>.Value = Form1.revenueTextBox.Text
                m.<runtime>.Value = Form1.runtimeTextBox.Text
                m.<original_name>.Value = Form1.movieNameTextBox.Text
                m.<language>.Value = "en"
            Next

'Music composers
            For Each p In movieData.<OpenSearchDescription>.<movies>.<movie>.<cast>.<person>

                'Music composer
                p.<name>.Value = Form1.musicComposerTextBox.Text
                p.<name>.<job>.Value = "Music Composer"

                Exit For

            Next

            'Screen play
            Dim screenNames(1) As String
            screenNames(0) = Form1.screenPlayTextBox1.Text
            screenNames(1) = Form1.screenPlayTextBox2.Text

            Dim j As Integer = 0
            For Each s In movieData.<OpenSearchDescription>.<movies>.<movie>.<cast>.<person>

                If j = 2 Then Exit For

                s.<name>.<job>.Value = "Screenplay"
                s.<name>.Value = screenNames(j)

                j = j + 1
            Next


            'directors
            Dim directorNames(1) As String
            directorNames(0) = Form1.director1NameTextBox.Text
            directorNames(1) = Form1.director2NameTextbox.Text

            Dim k As Integer = 0
            For Each d In movieData.<OpenSearchDescription>.<movies>.<movie>.<cast>.<person>

                If k = 2 Then Exit For

                d.<name>.Value = directorNames(k)
                d.<name>.<job>.Value = "Director"

                k = k + 1
            Next

            'Actors
            Dim actorNames(6) As String
            actorNames(0) = Form1.realNameTextBox1.Text
            actorNames(1) = Form1.realNameTextBox2.Text
            actorNames(2) = Form1.realNameTextBox3.Text
            actorNames(3) = Form1.realNameTextBox4.Text
            actorNames(4) = Form1.realNameTextBox5.Text
            actorNames(5) = Form1.realNameTextBox6.Text
            actorNames(6) = Form1.realNameTextBox7.Text

            Dim i As Integer = 0
            For Each a In movieData.<OpenSearchDescription>.<movies>.<movie>.<cast>.<person>

                If i = 7 Then Exit For

                'Actors
                a.<name>.Value = actorNames(i)
                a.<name>.<department>.Value = "Actors"

                i = i + 1
            Next

            'genre
            For Each g In movieData.<OpenSearchDescription>.<movies>.<movie>.<categories>.<category>

                g.<name>.Value = Form1.genresTextBox.Text
                Exit For
            Next

            'studios
            For Each s In movieData.<OpenSearchDescription>.<movies>.<movie>.<studios>.<studio>

                s.<name>.Value = Form1.studioNamesTextBox.Text
                Exit For
            Next


  movieData.Save(xmlFilePath)


        Catch ex As Exception

        End Try


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1