locked
Windows Store Application: Output of LINQ Query to Text Box on XAML Page (Visual Basic)

    Question

  • I am having issues getting any output to the text box.  I don't know what I am doing wrong, but in the books and web posts it seems I should be seeing something when I push the button on the page.  I'm not sure if the routine is written correctly to pull just the Vehicle Make and Model out of the XML and display the list of the entries that are for 1985.  On the MainPage.xaml I have a button that when pushed runs the following routine:

        Private Sub AddButton_Click(sender As Object, e As RoutedEventArgs) Handles AddButton.Click
            'add the name from the InputTextBox object to the collection
            StudentNames.Add(InputTextBox.Text.TrimEnd)
            'clear the new name from the text box and keep cursor there
            InputTextBox.Text = ""
            InputTextBox.Focus(Windows.UI.Xaml.FocusState.Programmatic)
        End Sub

        Private Sub RunQuery_Click(sender As Object, e As RoutedEventArgs) Handles RunQuery.Click
            OutputTextBox.Text = "Vehicles listed during 1985" & vbCrLf & vbCrLf

            Dim VehicleEPAData As XElement = XElement.Load("VehicleData/vehicle_epa_data_extract.xml")
            Dim queryData = From VehicleInfo In VehicleData.Elements("vehicles")
                          Where CStr(VehicleData.Element("vehicle").Element("year")) = "1985" _
                          Select MName = VehicleData.Element("vehicle").Element("make").Value,
                          MdName = VehicleData.Element("vehicle").Element("make").Value
            For Each scanResult In queryData
                OutputTextBox.Text &= queryData.MName & " " & queryData.MdName & "Say Cheese"
            Next
        End Sub

    The XML looks like this .... I have only provided the top two vehicles:8" standalone="yes"?>
    <vehicles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <vehicle>
      <barrels08>15.689436</barrels08>
      <barrelsA08>0</barrelsA08>
      <charge120>0</charge120>
      <charge240>0</charge240>
      <city08>19</city08>
      <city08U>0</city08U>
      <cityA08>0</cityA08>
      <cityA08U>0</cityA08U>
      <cityCD>0</cityCD>
      <cityE>0</cityE>
      <cityUF>0</cityUF>
      <co2>-1</co2>
      <co2A>-1</co2A>
      <co2TailpipeAGpm>0</co2TailpipeAGpm>
      <co2TailpipeGpm>423.190476190476</co2TailpipeGpm>
      <comb08>21</comb08>
      <comb08U>0</comb08U>
      <combA08>0</combA08>
      <combA08U>0</combA08U>
      <combE>0</combE>
      <combinedCD>0</combinedCD>
      <combinedUF>0</combinedUF>
      <cylinders>4</cylinders>
      <displ>2.0</displ>
      <drive>Rear-Wheel Drive</drive>
      <engId>9011</engId>
      <eng_dscr>(FFS)</eng_dscr>
      <feScore>-1</feScore>
      <fuelCost08>2600</fuelCost08>
      <fuelCostA08>0</fuelCostA08>
      <fuelType>Regular</fuelType>
      <fuelType1>Regular Gasoline</fuelType1>
      <ghgScore>-1</ghgScore>
      <ghgScoreA>-1</ghgScoreA>
      <highway08>25</highway08>
      <highway08U>0</highway08U>
      <highwayA08>0</highwayA08>
      <highwayA08U>0</highwayA08U>
      <highwayCD>0</highwayCD>
      <highwayE>0</highwayE>
      <highwayUF>0</highwayUF>
      <hlv>0</hlv>
      <hpv>0</hpv>
      <id>1</id>
      <lv2>0</lv2>
      <lv4>0</lv4>
      <make>Alfa Romeo</make>
      <model>Spider Veloce 2000</model>
      <mpgData>Y</mpgData>
      <phevBlended>false</phevBlended>
      <pv2>0</pv2>
      <pv4>0</pv4>
      <range>0</range>
      <rangeCity>0</rangeCity>
      <rangeCityA>0</rangeCityA>
      <rangeHwy>0</rangeHwy>
      <rangeHwyA>0</rangeHwyA>
      <trany>Manual 5-spd</trany>
      <UCity>23.3333</UCity>
      <UCityA>0</UCityA>
      <UHighway>35</UHighway>
      <UHighwayA>0</UHighwayA>
      <VClass>Two Seaters</VClass>
      <year>1985</year>
      <youSaveSpend>-1000</youSaveSpend>
     </vehicle>
     <vehicle>
      <barrels08>29.950562</barrels08>
      <barrelsA08>0</barrelsA08>
      <charge120>0</charge120>
      <charge240>0</charge240>
      <city08>9</city08>
      <city08U>0</city08U>
      <cityA08>0</cityA08>
      <cityA08U>0</cityA08U>
      <cityCD>0</cityCD>
      <cityE>0</cityE>
      <cityUF>0</cityUF>
      <co2>-1</co2>
      <co2A>-1</co2A>
      <co2TailpipeAGpm>0</co2TailpipeAGpm>
      <co2TailpipeGpm>807.909090909091</co2TailpipeGpm>
      <comb08>11</comb08>
      <comb08U>0</comb08U>
      <combA08>0</combA08>
      <combA08U>0</combA08U>
      <combE>0</combE>
      <combinedCD>0</combinedCD>
      <combinedUF>0</combinedUF>
      <cylinders>12</cylinders>
      <displ>4.9</displ>
      <drive>Rear-Wheel Drive</drive>
      <engId>22020</engId>
      <eng_dscr>(GUZZLER)</eng_dscr>
      <feScore>-1</feScore>
      <fuelCost08>5000</fuelCost08>
      <fuelCostA08>0</fuelCostA08>
      <fuelType>Regular</fuelType>
      <fuelType1>Regular Gasoline</fuelType1>
      <ghgScore>-1</ghgScore>
      <ghgScoreA>-1</ghgScoreA>
      <guzzler>T</guzzler>
      <highway08>14</highway08>
      <highway08U>0</highway08U>
      <highwayA08>0</highwayA08>
      <highwayA08U>0</highwayA08U>
      <highwayCD>0</highwayCD>
      <highwayE>0</highwayE>
      <highwayUF>0</highwayUF>
      <hlv>0</hlv>
      <hpv>0</hpv>
      <id>10</id>
      <lv2>0</lv2>
      <lv4>0</lv4>
      <make>Ferrari</make>
      <model>Testarossa</model>
      <mpgData>N</mpgData>
      <phevBlended>false</phevBlended>
      <pv2>0</pv2>
      <pv4>0</pv4>
      <range>0</range>
      <rangeCity>0</rangeCity>
      <rangeCityA>0</rangeCityA>
      <rangeHwy>0</rangeHwy>
      <rangeHwyA>0</rangeHwyA>
      <trany>Manual 5-spd</trany>
      <UCity>11</UCity>
      <UCityA>0</UCityA>
      <UHighway>19</UHighway>
      <UHighwayA>0</UHighwayA>
      <VClass>Two Seaters</VClass>
      <year>1985</year>
      <youSaveSpend>-13000</youSaveSpend>
     </vehicle>


    M. Frenchik Developer At Large



    Sunday, June 1, 2014 4:21 AM

Answers

  • A better version that allows for flexibility on the output side:


        Private Sub RunQuery_Click(sender As Object, e As RoutedEventArgs) Handles RunQuery.Click
            OutputTextBox.Text = "Vehicles listed during 1985" & vbCrLf & vbCrLf

            Dim VehicleDataXml As XElement = XElement.Load("Assets/epadatasmall.xml")
            Dim queryData = From VehicleInfo In VehicleDataXml.Elements("vehicle") _
                           Where VehicleInfo.Element("year").Value.Contains("1985") _
                           Select VehicleInfo
            For Each scanResult In queryData
                OutputTextBox.Text &= scanResult.Element("make").Value & " " & scanResult.Element("model").Value & vbCrLf
            Next
        End Sub


    M. Frenchik Developer At Large

    • Marked as answer by GoliathRulz Monday, June 2, 2014 4:05 PM
    Monday, June 2, 2014 1:33 AM

All replies

  • First thing is that your link query uses VehicleData but variable that holds XML is VehicleEPAData. Then in query should be written on VehicleData.Element("vehicles").Elements("vehicle"), this will need further changes in where clause. Please try to resolve this yourself, if not able then ask again.

    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!

    Sunday, June 1, 2014 12:47 PM
  • I know that you are hoping that with a push I can get over the hump, but I'm missing something (most likely simple).   I have to say thanks for the guidance, but I am just running into a wall on how to get this thing to do a simple output to the TextBox.   I have tried working with this for now 6 hours and have looked online at multiple sites, and in the programming book I have.  The reality is that the guidance is sort of vague, and there is nothing out there to ultimately (from what I can see) feed it back to the TextBox (going to a console is great but not helpful if you want to actually create an application).   If someone can just be really kind and get me a routine to start with that actually spits out text based on narrowing to a year of 1985 I think I will be in a better space.

    I'll keep working it today, but I'm getting really .... really ... frustrated.

    I have revised the routine to be, but it still doesn't work:

    Public NotInheritable Class MainPage
        Inherits Page
        Dim scanResult As Object
        Dim queryData As Object

        Private Sub AddButton_Click(sender As Object, e As RoutedEventArgs) Handles AddButton.Click
            'clear the new name from the text box and keep cursor there
            InputTextBox.Text = ""
            InputTextBox.Focus(Windows.UI.Xaml.FocusState.Programmatic)
        End Sub

        Private Sub RunQuery_Click(sender As Object, e As RoutedEventArgs) Handles RunQuery.Click
            OutputTextBox.Text = "Vehicles listed during 1985" & vbCrLf & vbCrLf

            Dim VehicleDataXml As XElement = XElement.Load("Assets/epadatasmall.xml")
            Dim queryData = From VehicleInfo In VehicleDataXml.Element("vehicles").Elements("vehicle")
                           Where CStr(VehicleInfo.Element("year")) = "1985" _
                           Select MName = VehicleInfo.Element("make").Value, _
                           MdName = VehicleInfo.Element("make").Value
            For Each scanResult In queryData
                OutputTextBox.Text &= queryData.MName & " " & queryData.MdName & vbCrLf
            Next
        End Sub
    End Class


    M. Frenchik Developer At Large


    • Edited by GoliathRulz Sunday, June 1, 2014 8:03 PM missed a )
    Sunday, June 1, 2014 7:57 PM
  • OK, I just got the query to work.  I now understand you don't call the root, but you call the XML starting at the first element level.  This said, I just need a bit of help calling the value of the MNName and MdName on the "For Each" statement.   So, the only line now that doesn't work is the one within the loop for OutputTextBox.Text.  The new routine part below with the working query:


        Private Sub RunQuery_Click(sender As Object, e As RoutedEventArgs) Handles RunQuery.Click
            OutputTextBox.Text = "Vehicles listed during 1985" & vbCrLf & vbCrLf

            Dim VehicleDataXml As XElement = XElement.Load("Assets/epadatasmall.xml")
            Dim queryData = From VehicleInfo In VehicleDataXml.Elements("vehicle") _
                           Where VehicleInfo.Element("year").Value.Contains("1985") _
                           Select MName = VehicleInfo.Element("make").Value, _
                           MdName = VehicleInfo.Element("make").Value
            For Each scanResult In queryData
                ' OutputTextBox.Text = queryData.MName & " " & queryData.MdName & vbCrLf
                OutputTextBox.Text &= vbCrLf & "Test Text" & vbCrLf
            Next


    M. Frenchik Developer At Large

    Monday, June 2, 2014 12:58 AM
  • OK, I got it to work all the way through from pulling the XML through to output into the Textbox with:


        Private Sub RunQuery_Click(sender As Object, e As RoutedEventArgs) Handles RunQuery.Click
            OutputTextBox.Text = "Vehicles listed during 1985" & vbCrLf & vbCrLf

            Dim VehicleDataXml As XElement = XElement.Load("Assets/epadatasmall.xml")
            Dim queryData = From VehicleInfo In VehicleDataXml.Elements("vehicle") _
                           Where VehicleInfo.Element("year").Value.Contains("1985") _
                           Select MName = VehicleInfo.Element("make").Value, _
                           MdName = VehicleInfo.Element("model").Value
            For Each scanResult In queryData
                OutputTextBox.Text &= scanResult.MName & " " & scanResult.MdName & vbCrLf
            Next


    M. Frenchik Developer At Large

    Monday, June 2, 2014 1:12 AM
  • A better version that allows for flexibility on the output side:


        Private Sub RunQuery_Click(sender As Object, e As RoutedEventArgs) Handles RunQuery.Click
            OutputTextBox.Text = "Vehicles listed during 1985" & vbCrLf & vbCrLf

            Dim VehicleDataXml As XElement = XElement.Load("Assets/epadatasmall.xml")
            Dim queryData = From VehicleInfo In VehicleDataXml.Elements("vehicle") _
                           Where VehicleInfo.Element("year").Value.Contains("1985") _
                           Select VehicleInfo
            For Each scanResult In queryData
                OutputTextBox.Text &= scanResult.Element("make").Value & " " & scanResult.Element("model").Value & vbCrLf
            Next
        End Sub


    M. Frenchik Developer At Large

    • Marked as answer by GoliathRulz Monday, June 2, 2014 4:05 PM
    Monday, June 2, 2014 1:33 AM