简体   繁体   中英

VB.NET XML Linq Get Descendants

I am trying to use LINQ to get the values of some child elements. Here's the XML file:

<?xml version="1.0" standalone="yes"?>
 <UserRecipes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <recipe name="Chocolate" portions="5">
   <ingredient quantity="500" unit="g">Chocolate Bar</ingredient>
   <ingredient quantity="300" unit="g">Chocolate Buttons</ingredient>
   <ingredient quantity="250" unit="g">Hot Chocolate</ingredient> 
  </recipe>
  <recipe name="Cookies" portions="24">
   <ingredient quantity="4" unit="oz">Flour</ingredient>
   <ingredient quantity="200" unit="g">Chocolate Buttons</ingredient>
   <ingredient quantity="600" unit="g">Sugar</ingredient>
  </recipe>
  <recipe name="Cake" portions="22">
   <ingredient quantity="4" unit="oz">Flour</ingredient>
   <ingredient quantity="4" unit="oz">Sugar</ingredient>
   <ingredient quantity="4" unit="oz">Butter</ingredient>
   <ingredient quantity="60" unit="n/a">Eggs</ingredient>
</recipe>
</UserRecipes>

The idea of this is to be able to retrieve the ingredients used in a specific recipe. For example, if the user chooses "Chocolate", then it would retrieve all the ingredients used in the "chocolate" recipe - in this case: Chocolate Bar, Chocolate Buttons and Hot Chocolate.

However, currently, when I perform my LINQ query, it returns all these 3 values as one long string. Eg Chocolate BarChocolate ButtonsHot Chocolate. Which isn't very useful, because I need these separate to add to my datatable (later on).

Here is the VB.NET code I currently have:

Dim Doc As XDocument = XDocument.Load("G:\Computing\!Recipe Test\XMLTest.xml")
Dim LinqQuery As IEnumerable(Of XElement) = From element In Doc.Descendants
                                                Where element.@name = "Chocolate"
                                                Select element
For Each Ele In LinqQuery
   RichTextBox1.AppendText(Ele.Value & ControlChars.NewLine)
Next

Which returns

Chocolate BarChocolate ButtonsHot Chocolate

in the textbox.

How can I get it to return each value separately (and therefore put them on a new line)

Many thanks - hope this makes sense, it's my first time with XML! Alternative methods are also allowed, more than happy to listen to other ideas and techniques.

The problem is that you are selecting the recipe element rather than all of the ingredient elements. When you output the Value of a parent element, it simply concatenates the text values of all of it's children, which is what you are seeing.

One simple way to fix this is to change this line:

Dim LinqQuery As IEnumerable(Of XElement) = From element In Doc.Descendants
                                                Where element.@name = "Chocolate"
                                                Select element

To this:

Dim LinqQuery As IEnumerable(Of XElement) = From element In Doc.Root.Descendants
                                                Where element.Parent.@name = "Chocolate"
                                                Select element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM