none
Getting only selected data from an XML url

    Question

  • This is my first time working with XML so I haven't a clue on it.

    What I am trying to do is get data for only part of the XML. I don't want all of it and so far every tutorial I have found only shows me how to grab all the data.

    I will give you an example of the data I am trying to grab.

    -<data>
    
    <total>12.70</total>
    
    -<item0>
    
    <date>2014-01-12</date>
    
    <banner_clicks>0</banner_clicks>
    
    <banner_impressions>0</banner_impressions>
    
    <banner_requests>0</banner_requests>
    
    <completion_rate>62</completion_rate>
    
    <ctr>0.00</ctr>
    
    <ecpm>2.60</ecpm>
    
    <googleadsense_requests>10</googleadsense_requests>
    
    <total_balance>0.3412</total_balance>
    
    <total_clicks>0</total_clicks>
    
    <total_error_impressions>86</total_error_impressions>
    
    <total_hits>217</total_hits>
    
    <total_hits_mousenotmoved>0</total_hits_mousenotmoved>
    
    <total_hits_nonhuman_percentage>0</total_hits_nonhuman_percentage>
    
    <total_impressions>81</total_impressions>
    
    <total_requests>131</total_requests>
    
    <video_clicks>0</video_clicks>
    
    <video_impressions>81</video_impressions>
    
    <video_impressions_t1>52</video_impressions_t1>
    
    <video_impressions_t2>12</video_impressions_t2>
    
    <video_impressions_t3>17</video_impressions_t3>
    
    <video_requests>121</video_requests>
    
    </item0>
    
    
    -<item1>
    
    <date>2014-01-11</date>
    
    <banner_clicks>0</banner_clicks>
    
    <banner_impressions>0</banner_impressions>
    
    <banner_requests>0</banner_requests>
    
    <completion_rate>51</completion_rate>
    
    <ctr>0.77</ctr>
    
    <ecpm>1.58</ecpm>
    
    <googleadsense_requests>144</googleadsense_requests>
    
    <total_balance>0.4335</total_balance>
    
    <total_clicks>1</total_clicks>
    
    <total_error_impressions>19</total_error_impressions>
    
    <total_hits>293</total_hits>
    
    <total_hits_mousenotmoved>0</total_hits_mousenotmoved>
    
    <total_hits_nonhuman_percentage>0</total_hits_nonhuman_percentage>
    
    <total_impressions>139</total_impressions>
    
    <total_requests>274</total_requests>
    
    <video_clicks>1</video_clicks>
    
    <video_impressions>139</video_impressions>
    
    <video_impressions_t1>58</video_impressions_t1>
    
    <video_impressions_t2>33</video_impressions_t2>
    
    <video_impressions_t3>48</video_impressions_t3>
    
    <video_requests>134</video_requests>
    
    </item1>

    So I want to only grab the value for total which is 12.70 and I want to grab total_balance for both item 0 and item 1.

    Can this be done or do I have to save this file and then open it with streamReader and search for the lines and so fourth.

    I am hoping there a way I can just tell it what values to grab and store to var's for me.

    Thanks,

    Scott

    Sunday, January 12, 2014 9:49 PM

Answers

  • Scott,

    Using LINQ-To-XML, it's not so hard to get whatever you want.

    As an example (and I had to modify what you posted some as it's malformed XML as you have it):

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim exampleDoc = <data> <total>12.70</total> <item0> <date>2014-01-12</date> <banner_clicks>0</banner_clicks> <banner_impressions>0</banner_impressions> <banner_requests>0</banner_requests> <completion_rate>62</completion_rate> <ctr>0.00</ctr> <ecpm>2.60</ecpm> <googleadsense_requests>10</googleadsense_requests> <total_balance>0.3412</total_balance> <total_clicks>0</total_clicks> <total_error_impressions>86</total_error_impressions> <total_hits>217</total_hits> <total_hits_mousenotmoved>0</total_hits_mousenotmoved> <total_hits_nonhuman_percentage>0</total_hits_nonhuman_percentage> <total_impressions>81</total_impressions> <total_requests>131</total_requests> <video_clicks>0</video_clicks> <video_impressions>81</video_impressions> <video_impressions_t1>52</video_impressions_t1> <video_impressions_t2>12</video_impressions_t2> <video_impressions_t3>17</video_impressions_t3> <video_requests>121</video_requests> </item0> <item1> <date>2014-01-11</date> <banner_clicks>0</banner_clicks> <banner_impressions>0</banner_impressions> <banner_requests>0</banner_requests> <completion_rate>51</completion_rate> <ctr>0.77</ctr> <ecpm>1.58</ecpm> <googleadsense_requests>144</googleadsense_requests> <total_balance>0.4335</total_balance> <total_clicks>1</total_clicks> <total_error_impressions>19</total_error_impressions> <total_hits>293</total_hits> <total_hits_mousenotmoved>0</total_hits_mousenotmoved> <total_hits_nonhuman_percentage>0</total_hits_nonhuman_percentage> <total_impressions>139</total_impressions> <total_requests>274</total_requests> <video_clicks>1</video_clicks> <video_impressions>139</video_impressions> <video_impressions_t1>58</video_impressions_t1> <video_impressions_t2>33</video_impressions_t2> <video_impressions_t3>48</video_impressions_t3> <video_requests>134</video_requests> </item1> </data> Dim xDoc = XElement.Parse(exampleDoc.ToString) Dim total As String = xDoc...<total>.Value Stop End Sub End Class


    There it's in memory so I used the .Parse method, but if it's a file or a URL that it's reading, use the .Load method instead. In that one I'm only showing getting the total but hopefully that'll give you ideas on how you might build a function to get what you want from it.

    I hope that helps. :)


    ***** Addendum *****

    Let me know if it's a file or a URL (or if not, just where it came from). If you're interested, I'll post a link to where you can upload the entire thing to me via my FTP site.

    I'll build something more complete (probably tomorrow, not tonight), but I need to work with the whole thing, not a random excerpt.

    Sunday, January 12, 2014 11:09 PM
  • I will have to check this after work as it to late tonight. It a URL. It really an API URL. When I go to it, it displays raw data in XML format in the browser. This data includes things such as earnings, number of hits and from where in the world, ad types, ect... Forgive my newbie side but the part where you got the value being stored into the var total. You got xDoc...<total>.Value What is the ..., 1st time I ever seen that in vb.

    First, have a look at the MSDN documentation for LINQ-To-XML. In that, you might want to look for the term axis properties. That's where the ellipsis comes from. I'll be the first to admit that it's an odd syntax, but that's how they set it up anyway.

    Given what you want, I saved the XML to a file and uploaded it to my website here and I'll use that in the following to give you an idea of how to get the other items that you asked for:

    Option Strict On Option Explicit On Public Class Form1 Private Enum ValueToReturn Total BalanceItem0 BalanceItem1 End Enum Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim exampleXML As String = _ "http://www.fls-online.com/VBNet_Forum/01-13-14/Example.xml" Dim total As Decimal = GetValueFromXML(exampleXML, ValueToReturn.Total) Dim balance0 As Decimal = GetValueFromXML(exampleXML, ValueToReturn.BalanceItem0) Dim balance1 As Decimal = GetValueFromXML(exampleXML, ValueToReturn.BalanceItem1) Stop ' Hover your mouse over the three variables ' above when the program halts at "Stop". End Sub Private Function GetValueFromXML(ByVal url As String, _ ByVal whatToGet As ValueToReturn) _ As Decimal Dim retVal As Decimal = 0 Try Dim xDoc = XElement.Load(url) Select Case whatToGet Case ValueToReturn.Total retVal = CDec(xDoc...<total>.Value) Case ValueToReturn.BalanceItem0 For Each item As XElement In xDoc...<item0> retVal = CDec(item...<total_balance>.Value) Next Case ValueToReturn.BalanceItem1 For Each item As XElement In xDoc...<item1> retVal = CDec(item...<total_balance>.Value) Next End Select Catch ex As Exception MessageBox.Show("An error occurred attempting to return the" & _ vbCrLf & "value for " & whatToGet.ToString & _ ":" & vbCrLf & vbCrLf & ex.Message, _ "Program Error", MessageBoxButtons.OK, _ MessageBoxIcon.Warning) End Try Return retVal End Function End Class


    In the form's .Load event, when it gets to the word "Stop", the program will halt just as through you'd put a breakpoint in. When it does, hover your mouse over the three variables there to see the values.

    I assumed you wanted them as decimals rather than strings, but change that if that's not the case.

    I hope it helps. :)


    Please call me Frank :)

    • Marked as answer by Elochai Tuesday, January 14, 2014 1:00 AM
    Monday, January 13, 2014 5:56 AM

All replies

  • Scott,

    Using LINQ-To-XML, it's not so hard to get whatever you want.

    As an example (and I had to modify what you posted some as it's malformed XML as you have it):

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim exampleDoc = <data> <total>12.70</total> <item0> <date>2014-01-12</date> <banner_clicks>0</banner_clicks> <banner_impressions>0</banner_impressions> <banner_requests>0</banner_requests> <completion_rate>62</completion_rate> <ctr>0.00</ctr> <ecpm>2.60</ecpm> <googleadsense_requests>10</googleadsense_requests> <total_balance>0.3412</total_balance> <total_clicks>0</total_clicks> <total_error_impressions>86</total_error_impressions> <total_hits>217</total_hits> <total_hits_mousenotmoved>0</total_hits_mousenotmoved> <total_hits_nonhuman_percentage>0</total_hits_nonhuman_percentage> <total_impressions>81</total_impressions> <total_requests>131</total_requests> <video_clicks>0</video_clicks> <video_impressions>81</video_impressions> <video_impressions_t1>52</video_impressions_t1> <video_impressions_t2>12</video_impressions_t2> <video_impressions_t3>17</video_impressions_t3> <video_requests>121</video_requests> </item0> <item1> <date>2014-01-11</date> <banner_clicks>0</banner_clicks> <banner_impressions>0</banner_impressions> <banner_requests>0</banner_requests> <completion_rate>51</completion_rate> <ctr>0.77</ctr> <ecpm>1.58</ecpm> <googleadsense_requests>144</googleadsense_requests> <total_balance>0.4335</total_balance> <total_clicks>1</total_clicks> <total_error_impressions>19</total_error_impressions> <total_hits>293</total_hits> <total_hits_mousenotmoved>0</total_hits_mousenotmoved> <total_hits_nonhuman_percentage>0</total_hits_nonhuman_percentage> <total_impressions>139</total_impressions> <total_requests>274</total_requests> <video_clicks>1</video_clicks> <video_impressions>139</video_impressions> <video_impressions_t1>58</video_impressions_t1> <video_impressions_t2>33</video_impressions_t2> <video_impressions_t3>48</video_impressions_t3> <video_requests>134</video_requests> </item1> </data> Dim xDoc = XElement.Parse(exampleDoc.ToString) Dim total As String = xDoc...<total>.Value Stop End Sub End Class


    There it's in memory so I used the .Parse method, but if it's a file or a URL that it's reading, use the .Load method instead. In that one I'm only showing getting the total but hopefully that'll give you ideas on how you might build a function to get what you want from it.

    I hope that helps. :)


    ***** Addendum *****

    Let me know if it's a file or a URL (or if not, just where it came from). If you're interested, I'll post a link to where you can upload the entire thing to me via my FTP site.

    I'll build something more complete (probably tomorrow, not tonight), but I need to work with the whole thing, not a random excerpt.

    Sunday, January 12, 2014 11:09 PM
  • I will have to check this after work as it to late tonight. It a URL. It really an API URL. When I go to it, it displays raw data in XML format in the browser. This data includes things such as earnings, number of hits and from where in the world, ad types, ect... Forgive my newbie side but the part where you got the value being stored into the var total. You got xDoc...<total>.Value What is the ..., 1st time I ever seen that in vb.
    Monday, January 13, 2014 3:15 AM
  • I will have to check this after work as it to late tonight. It a URL. It really an API URL. When I go to it, it displays raw data in XML format in the browser. This data includes things such as earnings, number of hits and from where in the world, ad types, ect... Forgive my newbie side but the part where you got the value being stored into the var total. You got xDoc...<total>.Value What is the ..., 1st time I ever seen that in vb.

    First, have a look at the MSDN documentation for LINQ-To-XML. In that, you might want to look for the term axis properties. That's where the ellipsis comes from. I'll be the first to admit that it's an odd syntax, but that's how they set it up anyway.

    Given what you want, I saved the XML to a file and uploaded it to my website here and I'll use that in the following to give you an idea of how to get the other items that you asked for:

    Option Strict On Option Explicit On Public Class Form1 Private Enum ValueToReturn Total BalanceItem0 BalanceItem1 End Enum Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim exampleXML As String = _ "http://www.fls-online.com/VBNet_Forum/01-13-14/Example.xml" Dim total As Decimal = GetValueFromXML(exampleXML, ValueToReturn.Total) Dim balance0 As Decimal = GetValueFromXML(exampleXML, ValueToReturn.BalanceItem0) Dim balance1 As Decimal = GetValueFromXML(exampleXML, ValueToReturn.BalanceItem1) Stop ' Hover your mouse over the three variables ' above when the program halts at "Stop". End Sub Private Function GetValueFromXML(ByVal url As String, _ ByVal whatToGet As ValueToReturn) _ As Decimal Dim retVal As Decimal = 0 Try Dim xDoc = XElement.Load(url) Select Case whatToGet Case ValueToReturn.Total retVal = CDec(xDoc...<total>.Value) Case ValueToReturn.BalanceItem0 For Each item As XElement In xDoc...<item0> retVal = CDec(item...<total_balance>.Value) Next Case ValueToReturn.BalanceItem1 For Each item As XElement In xDoc...<item1> retVal = CDec(item...<total_balance>.Value) Next End Select Catch ex As Exception MessageBox.Show("An error occurred attempting to return the" & _ vbCrLf & "value for " & whatToGet.ToString & _ ":" & vbCrLf & vbCrLf & ex.Message, _ "Program Error", MessageBoxButtons.OK, _ MessageBoxIcon.Warning) End Try Return retVal End Function End Class


    In the form's .Load event, when it gets to the word "Stop", the program will halt just as through you'd put a breakpoint in. When it does, hover your mouse over the three variables there to see the values.

    I assumed you wanted them as decimals rather than strings, but change that if that's not the case.

    I hope it helps. :)


    Please call me Frank :)

    • Marked as answer by Elochai Tuesday, January 14, 2014 1:00 AM
    Monday, January 13, 2014 5:56 AM
  • Yes it does frank and it seems to be a lot less coding as the other XML examples I seen that tell me to import the xml namespace and use xml classes to get the data I need.

    I will have to test this method using the URL's that the API dishes out for each set of sats. But now that I understand how to grab the values. I think I should be ok to construct this the way I want.

    If I run into any issues, I will post. I am hoping I wont but the URL is not a link to a file so to say but really raw XML that loads inside the web browser. I'd show you an example but the API URL code has information for my account which shouldn't be publicly shared.

    Thanks Frank I will keep you up to date on how it turns out.

    Tuesday, January 14, 2014 1:00 AM
  • Yes it does frank and it seems to be a lot less coding as the other XML examples I seen that tell me to import the xml namespace and use xml classes to get the data I need.

    I will have to test this method using the URL's that the API dishes out for each set of sats. But now that I understand how to grab the values. I think I should be ok to construct this the way I want.

    If I run into any issues, I will post. I am hoping I wont but the URL is not a link to a file so to say but really raw XML that loads inside the web browser. I'd show you an example but the API URL code has information for my account which shouldn't be publicly shared.

    Thanks Frank I will keep you up to date on how it turns out.


    I'm glad it helped. :)

    Please call me Frank :)

    Tuesday, January 14, 2014 2:45 AM
  • Sorry for the late update, had some things come up. Anyway today I got to work on the program and everything is working great so far. I can grab the data fine from the URL :)
    Friday, January 17, 2014 4:22 AM
  • Sorry for the late update, had some things come up. Anyway today I got to work on the program and everything is working great so far. I can grab the data fine from the URL :)

    Sorry for the delay - I had an early morning meeting I'm just now getting back from.

    I'm glad it helped. :)


    Please call me Frank :)

    Friday, January 17, 2014 3:41 PM