0

I'm trying to select elements based on the attributes of sub a element of that element.

Original Xml:

 <Root>
      <Element1 id="1">
        <element2>XXXX</element2>
        <element2>XXXX</element2>
        <element2>XXXX</element2>
        <Filter attr1="1" attr2="0" attr3="0" attr4="1" attr5="0"></Filter>
      </Element1>
      <Element1 id="2">
        <element2>XXXX</element2>
        <element2>XXXX</element2>
        <element2>XXXX</element2>
        <Filter attr1="1" attr2="0" attr3="0" attr4="1" attr5="0"></Filter>
      </Element1>
      <Element1 id="3">
        <element2>XXXX</element2>
        <element2>XXXX</element2>
        <element2>XXXX</element2>
        <Filter attr1="1" attr2="0" attr3="0" attr4="1" attr5="0"></Filter>
      </Element1>
    </Root>

This is what I've done so far:

Dim xmlElement = (From rec In RecipeXmlDocument.Descendants("RECEPT") _
Where (rec.Descendants("Filter").@poultry = Ing.Poultry.ToString() _
                   Or rec.Descendants("Filter").@attr1 = "1" _
                   Or rec.Descendants("Filter").@attr2 = "0" _
                   And (rec.Descendants("Filter").@attr3 = "1" _
                        Or rec.Descendants("Filter").@attr4 = "0" _
                        Or rec.Descendants("Filter").@attr5 = "1"

This throws the following error: "Exception of type 'System.Linq.SystemCore_EnumerableDebugViewEmptyException' was thrown."

What the code is trying to do is to select all the Element1s where the filter element of that element1 matches the where clause of the statement.

I'm fairly new to linq and i'm not exactly sure what I'm doing wrong.

Martinffx
  • 2,426
  • 4
  • 33
  • 60
  • 1
    Please post complete samples allowing us to reproduce the problem. Doing `RecipeXmlDocument.Descendants("RECEPT")` with your posted sample document seems rather odd, considering the posted sample does not contain any elements named "RECEPT". To explain the error you get we need to see enough information to reproduce it. – Martin Honnen Mar 11 '11 at 11:32
  • Sorry, the "RECEPT" element is "Element1" in the example Xml I posted. – Martinffx Mar 14 '11 at 07:14

1 Answers1

0

What exactly do you want to select from the xml? The select statement is missing from your linq statement. Add the Select at the end of your linq statement, e.g Select (whatever from the tag)

Additionally, you are navigating "over" the element you are trying to search, ie

From rec In RecipeXmlDocument.Descendants("RECEPT")

should be

From rec In RecipeXmlDocument.Descendants

Below is an amended statement to which you need to concatenate your select statement

Dim xmlElement = From rec In RecipeXmlDocument.Descendants Where rec...<Filter>.@poultry = Ing.Poultry.ToString Or rec...<Filter>.@attr1 = "1" Or rec...<Filter>.@attr2 = "0" And (rec...<Filter>..@attr3 = "1" Or rec...<Filter>.@attr4 = "0" Or rec...<Filter>.@attr5 = "1")

NOTE:You will have to add some elements inside the tag to be able to select them