9 Replies - 1182 Views - Last Post: 06 October 2012 - 04:42 PM Rate Topic: -----

#1 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

edit .xml file

Posted 05 October 2012 - 03:32 PM

Hi guys,

im tryng to search for a value in an xml and edit it. I can get the first vale but not all of them

  'Music composers, loop cast until job is Music Composer. Update value and exit for
            For Each p In movieData.<OpenSearchDescription>.<movies>.<movie>.<cast> 
              
  If p.<job>.Value = "Original Music Composer" Then
                    'Music composer
                    p.<name>.Value = Form1.musicComposerTextBox.Text
                    Exit For
                End If


            Next



I can get this string, its the first of 10, but i can only get the first one, how do i loop threw them?
  <person name="Wilhelm von Homburg" character="Vigo" job="Actor" id="27585" thumb="" department="Actors" url="http://www.themoviedb.org/person/27585" order="12" cast_id="23" /> 




Kevin

This post has been edited by deery5000: 05 October 2012 - 03:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: edit .xml file

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: edit .xml file

Posted 05 October 2012 - 04:11 PM

You need to get the right elements.

movieData...<person>


Then check the name attribute.
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: edit .xml file

Posted 05 October 2012 - 04:28 PM

<person name="Wilhelm von Homburg" character="Vigo" job="Actor" id="27585" thumb="" department="Actors" url="http://www.themoviedb.org/person/27585" order="12" cast_id="23" />


In that snippet the job is an attribute, so use p.@job = "actor" .
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: edit .xml file

Posted 05 October 2012 - 04:56 PM

Or you can still use the .Where extension method, to further filter the data.
For Each p In movieData.<OpenSearchDescription>.<movies>.<movie>.<cast>.Where(Function(p) p.@job = "Original Music Composer")
 p.@name = Form1.musicComposerTextBox.Text
Next



or in query style
For Each p in (From m in movieDate.<OpenSearchDescription>.<movies>.<movie>
               From cm in m
               Where cm.@job ="Original Music Composer"
               Select m)
  p.@name = Form1.musicComposerTextBox.Text
Next 


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

Was This Post Helpful? 0
  • +
  • -

#5 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: edit .xml file

Posted 06 October 2012 - 02:43 PM

HAWK you were right i needed to specify the .<person> in my search then i could traverse them

Heres the revised code
'Music composers, loop cast until job is Music Composer. Update value and exit for  
            For Each p In movieData.<OpenSearchDescription>.<movies>.<movie>.<cast>.<person>

                If p.@job = "Original Music Composer" Then

                    'Music composer  
                    p.@name = Form1.musicComposerTextBox.Text

                    Exit For

                End If
            Next



Thanks for you help, i live ling to xml, very powerful tool

Kevin
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: edit .xml file

Posted 06 October 2012 - 02:46 PM

What a waste of a for each loop.
Was This Post Helpful? 0
  • +
  • -

#7 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: edit .xml file

Posted 06 October 2012 - 03:41 PM

Yes your are correct, this has been revised. the for each loop has been used like this

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

                'Music composers
                If p.@job = "Original Music Composer" Then

                    'Update Music composer value
                    p.@name = Form1.musicComposerTextBox.Text

                End If

                'Screenplay
                If p.@job = "Screenplay" Then

                    'Update Music composer value
                    p.@name = screenNames(j)
                    j = j + 1
                End If

  
                'Directors
                If p.@job = "Director" Then

                    'Update Music composer value
                    p.@name = directorNames(k)
                    k = k + 1
                End If

                'Actors
                If p.@job = "Actor" Then

                    If i = 7 Then Exit For

                    p.@name = actorNames(i)
                    i = i + 1
                End If

            Next



does that suit you better, im always open to criticism.

This post has been edited by deery5000: 06 October 2012 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#8 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: edit .xml file

Posted 06 October 2012 - 03:57 PM

You might want to switch from multiple If clauses to Select Case.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: edit .xml file

Posted 06 October 2012 - 04:17 PM

Seem like a lot a of similar stuff there, wouldn't that suggest writing method. Also the code doesn't convey it's intent, very well.

Sub ReplaceNameBasedOnJob(of T)(byref x As T, JobTitle As String, ReplacedName As Func(Of String)) As T
 If x.@job = JobTitle Then x.@name = ReplacedName()
End Sub

Sub ReplaceComposorWithNameFromComposerTextBox(of T)(byref x As T)
 ReplaceNameBasedOnJob(of T)(x, "Original Music Composer",
  Function()
    Return Form1.musicComposerTextBox.Text
  End Function)
End Sub
etc



Seem like Data and GUI is tightly coupled, separation helps.

This post has been edited by AdamSpeight2008: 06 October 2012 - 04:19 PM

Was This Post Helpful? 0
  • +
  • -

#10 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: edit .xml file

Posted 06 October 2012 - 04:42 PM

the idea of the function is to allow the user to manipulate the data on the form and ill update the xml file.

thanks for all the suggestions :)

kevin
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1