Read Xml data problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kennypt
    New Member
    • Mar 2009
    • 5

    Read Xml data problem

    Hi all, this is my first post, i have a little problem.

    I'm tryign to read a specific atribute from a xml website using this code:

    ...
    Code:
    'this is the url im trying to read, tmpIn is a string that complete the url:
    Dim url As String = "http://maps.google.com/maps/geo?q=" & tmpIn & "&output=xml"
    
    Dim reader As New XmlTextReader(url)
    
    reader.WhitespaceHandling = WhitespaceHandling.Significant
    
    While (reader.Read())
            'now i try to read the atribute coordinates from the url to a string tmpLatLong:
                If reader.Name.ToString() = "coordinates" Then
                    tmpLatLong = reader.ReadString().ToString()
                End If
    
     End While
    ...

    The problem is tmpIn takes diferents values (string) when the function is used and sometimes this works, and othertimes it gives an error (Invalid character in the given encoding. Line 9, position 15.), but is not because of the url, is when it tries to read the coordinates... any ideas how can i fix this? or change to a better code? any help is welcome..

    thx in advance
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    So what you're saying is if the TempIn is properly formated as Coordinates the function works, but if the TempIn is not properly formatted then the function fails?

    Do I understand that right?

    Comment

    • kennypt
      New Member
      • Mar 2009
      • 5

      #3
      Originally posted by tlhintoq
      So what you're saying is if the TempIn is properly formated as Coordinates the function works, but if the TempIn is not properly formatted then the function fails?

      Do I understand that right?
      the coordinates is given by the website, they depend of the TempIn value, but i dont know why it fails, for example if TempIn="canedo, santa maria da feira, portugal" (its a valid adress) it works fine, but if it is TempIn="lobao, santa maria da feira, portugal" (its a valid adress too) it doesnt work and gives that error (Invalid character in the given encoding. Line 9, position 15.) but i checking the website i dont see any cause that could make it happen, and i see the coordinates there with a valid value :S

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Ok... Check that I have this right..

        You feed the URL to XMLTextReader, which received an XML page back based on the location you give it in the TempIn variable.
        When you get the whole XML page back as a reply that goes into the While loop.
        When the While loop finally finds a tag of "coordinate s" it assigns them to tmpLatLong.

        But sometimes it goes awry... at you get an error in line 9, position 15.

        So what does line 9 of the response look like in a good value and what does line 9 of a bad run look like? What is at position 15 of the bad value?

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          I do notice that

          Gives a response where no characters have accented characters in it.
          canedo,santa maria da feira, portugal 200 geocode Canedo, Santa Maria da Feira, Portugal PTPortugalAveir oSanta Maria da FeiraCanedo -8.4630566,41.01 18083,0


          On the other hand does have accented a with tilde over it in a couple places.
          lobao, santa maria da feira, portugal 200 geocode Lobão, Santa Maria da Feira, Portugal PTPortugalAveir oSanta Maria da FeiraLobão -8.4894589,40.98 42033,0

          I would bet those are your illegal characters.

          Comment

          • kennypt
            New Member
            • Mar 2009
            • 5

            #6
            Originally posted by tlhintoq
            Ok... Check that I have this right..

            You feed the URL to XMLTextReader, which received an XML page back based on the location you give it in the TempIn variable.
            When you get the whole XML page back as a reply that goes into the While loop.
            When the While loop finally finds a tag of "coordinate s" it assigns them to tmpLatLong.

            But sometimes it goes awry... at you get an error in line 9, position 15.

            So what does line 9 of the response look like in a good value and what does line 9 of a bad run look like? What is at position 15 of the bad value?
            yes is how you say :)

            well, for example of a non working case, where line 9 is:
            Code:
            <address>Lobão, Santa Maria da Feira, Portugal</address>
            where in the line 21 i have
            Code:
            <coordinates>-8.4894589,40.9842033,0</coordinates>
            that is the information i need...
            idk if it is bcause of the char "ã", but i cant change that.

            a working case would be like:
            Code:
            <address>Canedo, Santa Maria da Feira, Portugal</address>
            and in line 21
            Code:
            <coordinates>-8.4630566,41.0118083,0</coordinates>
            but im not sure if that kind of characters are the cause of the error

            Comment

            • kennypt
              New Member
              • Mar 2009
              • 5

              #7
              Originally posted by tlhintoq
              I do notice that

              Gives a response where no characters have accented characters in it.
              canedo,santa maria da feira, portugal 200 geocode Canedo, Santa Maria da Feira, Portugal PTPortugalAveir oSanta Maria da FeiraCanedo -8.4630566,41.01 18083,0


              On the other hand does have accented a with tilde over it in a couple places.
              lobao, santa maria da feira, portugal 200 geocode Lobão, Santa Maria da Feira, Portugal PTPortugalAveir oSanta Maria da FeiraLobão -8.4894589,40.98 42033,0

              I would bet those are your illegal characters.
              maybe, but how i ignore that?

              Comment

              • kennypt
                New Member
                • Mar 2009
                • 5

                #8
                Ok i fixed the problem :D

                i added to the url &oe=utf-8, looking like this:
                Dim url As String = "http://maps.google.com/maps/geo?q=" & tmpIn & "&output=xml&oe =utf-8"

                and solved my problem.

                Thx for the help :)

                Comment

                Working...