Skip to main content
Home Forums Silverlight Programming Programming with .NET - General LINQ - XML Query
2 replies. Latest Post by mebjen on November 25, 2008.
(0)
mebjen
Member
82 points
99 Posts
11-25-2008 8:15 AM |
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 SubEnd 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
bryant
Star
9927 points
1,629 Posts
11-25-2008 9:33 AM |
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.
11-25-2008 10:13 AM |
Bryant,
Thank-you - I see now, what I was try to return wasn't ever going to work because of the XML node structure.