Skip to main content

Microsoft Silverlight

Answered Question LINQ - XML QueryRSS Feed

(0)

mebjen
mebjen

Member

Member

82 points

99 Posts

LINQ - XML Query

I am trying to retrieve data from XML - 

Here is my VB .NET Code:

Module Module1
    Sub Main()
        Dim myXML = <?xml version="1.0" encoding="utf-8"?>
                    <ctlData>
                        <tlData>
                            <customer>Bob's Farm</customer>
                            <createdOn>2006-11-08T16:36:35</createdOn>
                            <qty>1</qty>
                            <desc>Liquid Feed</desc>
                            <containerSizeInGallons>55</containerSizeInGallons>
                            <gallonsShipped>55</gallonsShipped>
                            <shipToState>CA</shipToState>
                            <shipToCountry>USA</shipToCountry>
                        </tlData>
                        <tlData>
                            <customer>Steve's Farm</customer>
                            <createdOn>2006-11-08T16:36:35</createdOn>
                            <qty>1</qty>
                            <desc>Liquid Feed</desc>
                            <containerSizeInGallons>6</containerSizeInGallons>
                            <gallonsShipped>55</gallonsShipped>
                            <shipToState>WA</shipToState>
                            <shipToCountry>USA</shipToCountry>
                        </tlData>
                        <tlData>
                            <customer>Rienhard's Farm</customer>
                            <createdOn>2006-11-08T16:36:35</createdOn>
                            <qty>5</qty>
                            <desc>Liquid Feed</desc>
                            <containerSizeInGallons>55</containerSizeInGallons>
                            <gallonsShipped>55</gallonsShipped>
                            <shipToState>NULL</shipToState>
                            <shipToCountry>Germany</shipToCountry>
                        </tlData>
                        <tlData>
                            <customer>Paul's Farm</customer>
                            <createdOn>2006-11-08T16:36:35</createdOn>
                            <qty>1</qty>
                            <desc>Liquid Fertilizer</desc>
                            <containerSizeInGallons>55</containerSizeInGallons>
                            <gallonsShipped>55</gallonsShipped>
                            <shipToState>OR</shipToState>
                            <shipToCountry>USA</shipToCountry>
                        </tlData>
                    </ctlData>

        Dim nvCust = <XData>
                         <%= From tldata In myXML...<customer> _
                             Where tldata.<shipToState>.Value = "OR" _
                             Select tldata %>
                     </XData>

        My.Computer.FileSystem.WriteAllText("nvCust.xml", nvCust.ToString, False)
        Process.Start("nvCust.xml")
    End Sub
End Module

When I run the above code I get  a browser page opened with only -     <XData />

However, If I change to this and run:

  Dim nvCust = <XData>
                         <%= From tldata In myXML...<customer> _
                                  Select tldata %>
                     </XData>

 I get in a browser page:

<XData>
  <customer>Bob's Farm</customer>
  <customer>Steve's Farm</customer>
  <customer>Rienhard's Farm</customer>
  <customer>Paul's Farm</customer>
</XData>

What am I doing wrong in the where clause?

 

Thanks

mb

bryant
bryant

Star

Star

9927 points

1,629 Posts

Silverlight MVP
Answered Question

Re: LINQ - XML Query

I'm not real familiar with the VB.NET syntax, but it looks like you're selecting the <customer> node, not the parent node. So I believe you would need something more like (excuse my syntax):

Dim nvCust = <XData>
                         <%= From tldata In myXML...<tlData> _
                             Where tldata.<shipToState>.Value = "OR" _
                             Select tldata.<customer> %>
                     </XData>

 Otherewise the tldata = <customer> which doesn't have a <shipToState> node under it.

-- bryant

Blog | Twitter
_________________
Dont forget to click "Mark as Answer" on the post that helped you.

mebjen
mebjen

Member

Member

82 points

99 Posts

Re: LINQ - XML Query

 Bryant,

Thank-you - I see now, what I was try to return wasn't ever going to work because of the XML node structure.

 

mb
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities