locked
Problems reading elements from XML RRS feed

  • Question

  • Hello everyone!! I am working with MediaFire API and I need to read XML online, from a specific request url

    I will leave and example of the response, codified in XML, when I load the request_url

    <response>
    <action>user/get_session_token</action>
    <session_token>
    1dbfa6bfe8b4fbaf0d9b4df6bb062bfd96380155a439b288181fc6a81338257976eae158683fbee18fb23121ddf2e29f7d8fcc0639f018ac29aa7984d82e9156887312499774e997
    </session_token>
    <secret_key>2034564729</secret_key>
    <time>1406222550.0564</time>
    <ekey>2adda8ea02818b0254e86381f7a17901</ekey>
    <pkey>0bf5c04f23</pkey>
    <result>Success</result>
    <current_api_version>1.0</current_api_version>
    </response>

    I need to read the following elements:

    • Session_token
    • Time
    • Secret_Key

    I am using the following code:

    Dim reader As XmlTextReader = New XmlTextReader(request_url) 
            While reader.Read
                If reader.NodeType = Xml.XmlNodeType.Element Then
                    Select Case reader.Name
                        Case "session_token"
                            session_token = reader.ReadElementContentAsString
                        Case "secret_key"
                            secret_key = reader.ReadElementContentAsString
                        Case "time"
                            time = reader.ReadElementContentAsString
                    End Select
                End If
            End While
    
    'session_token; secret_key; time
    'all are variables defined as String
    'Dim time as String = ""

    It's funny because I don't retrieve the value of the element secret_key 

    I did some experiences and I added a Breakpoint like in the image

    Then I debugged, and guess what!! Any break, witch means my code is not detecting the element "secret_key" and so, don't retrive the element value.

    Now, some curious facts!!

    This example here, works fine and shows that axially the code I have shown, detects all the elements I am looking for.

    Dim reader As XmlTextReader = New XmlTextReader(request_url)
            While reader.Read
                If reader.NodeType = Xml.XmlNodeType.Element Then
                    Select Case reader.Name
                        Case "session_token"
                            Msgbox("session token!!")
                        Case "secret_key"
                            Msgbox("Key!!")
                        Case "time"
                            Msgbox("Time!!")
                    End Select
                End If
            End While

    The three MesageBox are shown!!

    Back to the initial code,

    While reader.Read
                If reader.NodeType = Xml.XmlNodeType.Element Then
                    Select Case reader.Name
                        Case "session_token"
                            session_token = reader.ReadElementContentAsString
                        Case "secret_key"
                            secret_key = reader.ReadElementContentAsString
                        Case "time"
                            time = reader.ReadElementContentAsString
                    End Select
                End If
            End While

    It looks like after retrive the session_token element value, it setps over the secret_key, and read all the rest of the elements.

    Why? Any alternative solution?


    Meu novo programa (Bloco de Notas) http://hyrokumata-app.blogspot.pt/2012/07/notepad-3-beta.html

    Thursday, July 24, 2014 5:57 PM

Answers

  • Fabio,

    That's not the entire XML so without it, it's hard to give you specific help, but in short - I'd encourage you to use LINQ-To-XML to read it (directly via the URL).

    It shouldn't be difficult but without seeing the entire XML file, I can't really know.


    Still lost in code, just at a little higher level.

    :-)

    Thursday, July 24, 2014 6:37 PM

All replies

  • Fabio,

    That's not the entire XML so without it, it's hard to give you specific help, but in short - I'd encourage you to use LINQ-To-XML to read it (directly via the URL).

    It shouldn't be difficult but without seeing the entire XML file, I can't really know.


    Still lost in code, just at a little higher level.

    :-)

    Thursday, July 24, 2014 6:37 PM
  • Hello,

    Using the xml you supplied we can do the following where in this case I have placed the xml into My.Resources as a text file (all that matters is we are dealing with text).

    Code

    Results


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Proposed as answer by Quanta Friday, July 25, 2014 1:29 PM
    Thursday, July 24, 2014 7:44 PM
  • Fabio,

    That's not the entire XML so without it, it's hard to give you specific help, but in short - I'd encourage you to use LINQ-To-XML to read it (directly via the URL).

    It shouldn't be difficult but without seeing the entire XML file, I can't really know.


    Still lost in code, just at a little higher level.

    :-)

    Sorry Frank, probably that XML is not very well structured but that is the full response I ggrt

    You can see that is according with Media fire API CORE documentation. Nothing missing ...

    https://www.mediafire.com/developers/core_api/unversioned/

    I have a question ... I don't know very well the structure of a XML file but I was thinking in something. Because I know what exactly elements I need, is not there a way to retrieve the value by specifying the "path", I mean 

    response/session_token

    ?


    Meu novo programa (Bloco de Notas) http://hyrokumata-app.blogspot.pt/2012/07/notepad-3-beta.html

    Friday, July 25, 2014 7:07 AM
  • Hello,

    Using the xml you supplied we can do the following where in this case I have placed the xml into My.Resources as a text file (all that matters is we are dealing with text).

    Code

    Results


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Interesting ... I have to test it and try to understand that code. For sure, I will need to download the online response as a text file to somewhere location on hard drive and then work with it. I can't add it to My.Resources

    I am just thinking if is good idea converting a XML to a text file. Is there no problem?


    Meu novo programa (Bloco de Notas) http://hyrokumata-app.blogspot.pt/2012/07/notepad-3-beta.html

    Friday, July 25, 2014 7:11 AM
  • Windows Communication Foundation is made with mainly your problem in mind.

    Take a look at it.

    http://msdn.microsoft.com/en-us/library/ms731190(v=vs.110).aspx


    Success
    Cor

    Friday, July 25, 2014 8:39 AM
  • Fabio,

    That's not the entire XML so without it, it's hard to give you specific help, but in short - I'd encourage you to use LINQ-To-XML to read it (directly via the URL).

    It shouldn't be difficult but without seeing the entire XML file, I can't really know.


    Still lost in code, just at a little higher level.

    :-)

    Hello Frank!!

    I heard your sugesttion about using LINQ, well I tried and that solved my problem :) Thanks

    Dim root As XElement = XDocument.Load(request_url).Root
            session_token = root.Element("session_token").Value
            secret_key = root.Element("secret_key").Value
            time = root.Element("time").Value
    Source: [http://stackoverflow.com/questions/11581692/how-to-read-xml-elements-in-vb-net]

    Friday, July 25, 2014 9:09 AM
  • Hello Frank!!

    I heard your sugesttion about using LINQ, well I tried and that solved my problem :) Thanks

    Dim root As XElement = XDocument.Load(request_url).Root
            session_token = root.Element("session_token").Value
            secret_key = root.Element("secret_key").Value
            time = root.Element("time").Value
    Source: [http://stackoverflow.com/questions/11581692/how-to-read-xml-elements-in-vb-net]

    That's not how I would have done it but if it works for you then great! :)

    Still lost in code, just at a little higher level.

    :-)

    Friday, July 25, 2014 1:26 PM
  • Hello,

    Using the xml you supplied we can do the following where in this case I have placed the xml into My.Resources as a text file (all that matters is we are dealing with text).

    Code

    Results


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Interesting ... I have to test it and try to understand that code. For sure, I will need to download the online response as a text file to somewhere location on hard drive and then work with it. I can't add it to My.Resources

    I am just thinking if is good idea converting a XML to a text file. Is there no problem?


    Meu novo programa (Bloco de Notas) http://hyrokumata-app.blogspot.pt/2012/07/notepad-3-beta.html


    The concept is to read in a string containing valid xml into a XDocument. Does not matter where the data comes from, in my example think of the text file as mocked data that in a real app would had come from say downloading a file from the web etc.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, July 25, 2014 2:06 PM
  • I am just thinking if is good idea converting a XML to a text file. Is there no problem?

    Fabio,

    Please understand that an XML file already *is* a text file! It's just specially structured text but it's text nevertheless.

    What Kevin showed used LINQ, and it's not easy to catch on to at first, but it's very effective once you get the hang of it.

    *****

    Let me give you a more simple one to follow, even though this will also be somewhat confusing at first. I took the XML from what you posted originally and I put it into a text file that I named "Example.xml" and put that on my desktop. You might want to do the same so you can try this:

    Option Strict On Option Explicit On Imports System.IO.Path Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim dt As String = My.Computer.FileSystem.SpecialDirectories.Desktop Dim xmlFilePath As String = Combine(dt, "Example.xml") GetDataFromXML(xmlFilePath) End Sub Private Sub GetDataFromXML(ByVal urlOrFilePath As String) Try Dim xDoc As XElement = XElement.Load(url) Dim action As String = xDoc...<action>.Value Dim sessionToken As String = xDoc...<session_token>.Value Dim secretKey As String = xDoc...<secret_key>.Value Dim time As String = xDoc...<time>.Value Dim eKey As String = xDoc...<ekey>.Value Dim pKey As String = xDoc...<pkey>.Value Dim result As String = xDoc...<result>.Value Dim currentApiVersion As String = xDoc...<current_api_version>.Value Stop Catch ex As Exception MessageBox.Show("An error occurred:" & _ vbCrLf & vbCrLf & ex.Message, _ "Error Reading XML", _ MessageBoxButtons.OK, _ MessageBoxIcon.Warning) End Try End Sub End Class


    Notice in the sub that first it reads the XML file using XElement.Load.

    Because of the structure (this is why I wanted to see the whole thing - there's no telling how many levels there actually are), since it has a parent - <response> - then even though that's read, you won't use it directly; instead read the children of <response> like I show above.

    The odd looking ellipsis, well that's how LINQ-To-XML works (they're called axis accessors by the way) and of course, per item, you tell it specifically that you want to get the value of that element.

    Does that help some?


    Still lost in code, just at a little higher level.

    :-)

    Friday, July 25, 2014 6:57 PM