Results 1 to 6 of 6

Thread: [RESOLVED] get values from xml elements

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Location
    San Marcos, TX
    Posts
    177

    Resolved [RESOLVED] get values from xml elements

    Here's a portion of my XML string.

    <OpenSearchDescription>
    <opensearch:Query searchTerms="Star Wars"/>
    <opensearch:totalResults>18</opensearch:totalResults>

    Here's my code:
    Code:
    Dim xml As String = tmdb_.Movie_search(tbTitle.Text, cbYear.Text)
    Dim xdoc As XDocument = XDocument.Parse(xml)
    
    lbResults.Text = xdoc.Element("totalResults").Value.ToString
    The line "Dim xml As String = tmdb_.Movie_search(tbTitle.Text, cbYear.Text)" produces a string with the entire xml return. How do I go about setting lbResult.Text to "18" or the value of "<opensearch:totalResults>"

    Thanks!

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Location
    San Marcos, TX
    Posts
    177

    Re: get values from xml elements

    No ideas?

  3. #3
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: get values from xml elements

    I think you need to specify the full Xpath. Maybe something like:

    Code:
    lbResults.Text = xdoc.Element("OpenSearchDescription").Element("opensearch:totalResults").Value
    And you dont need the .ToString at the end as .Value is already a string.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Location
    San Marcos, TX
    Posts
    177

    Re: get values from xml elements

    Thanks for the reply, but running it I get the following error.

    "The ':' character, hexadecimal value 0x3A, cannot be included in a name."

    Any thoughts?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Location
    San Marcos, TX
    Posts
    177

    Re: get values from xml elements

    I've been reading that I need to use a XNamespace, how would I go about doing this?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Location
    San Marcos, TX
    Posts
    177

    Re: get values from xml elements

    Woohoo I did it.

    Code:
    Imports <xmlns="http://a9.com/-/spec/opensearch/1.1/">
    
    In the function:
    Dim xname As XNamespace = "http://a9.com/-/spec/opensearch/1.1/"
    lbResults.Text = xdoc.Element("OpenSearchDescription").Descendants(xname + "totalResults").Value

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