user961794
user961794

Reputation: 111

LINQ to XML. Enumeration yielded no results

I'm having trouble populating an object from an XML file. I've copied an example I've found almost exactly, with variable names changed, but I keep getting the "Enumeration yielded no results" exception.

Here is my code:

    Dim element As XElement = XElement.Load(path)

    Dim itemProps = From p In element...<Property> _
                Where p.<LanguageCode>.Value = "en_us" _
                Select p.<Title>.Value, p.<Description>.Value

Using breakpoints, I have confirmed that the 'element' variable is being properly populated using the XElement.Load(path) method.

Here is the XML file that is being accessed:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Items>
  <Item ItemID="1">
    <Property ItemPropertyID="1">
      <Title>Title1</Title>
      <Description>Description1</Description>
      <LanguageCode>en-us</LanguageCode>
    </Property>
  </Item>
  <Item ItemID="2">
    <Property ItemPropertyID="2">
      <Title>Title2</Title>
      <Description>Description2</Description>
      <LanguageCode>en-us</LanguageCode>
    </Property>
  </Item>
  <Item ItemID="3">
    <Property ItemPropertyID="3">
      <Title>Title3</Title>
      <Description>Description3</Description>
      <LanguageCode>en-us</LanguageCode>
    </Property>
  </Item>
  <Item ItemID="4">
    <Property ItemPropertyID="4">
      <Title>Title4</Title>
      <Description>Description4</Description>
      <LanguageCode>en-us</LanguageCode>
    </Property>
  </Item>
  <Item ItemID="5">
    <Property ItemPropertyID="5">
      <Title>Title5</Title>
      <Description>Description5</Description>
      <LanguageCode>en-us</LanguageCode>
    </Property>
  </Item>
  <Item ItemID="6">
    <Property ItemPropertyID="6">
      <Title>Title6</Title>
      <Description>Description6</Description>
      <LanguageCode>en-us</LanguageCode>
    </Property>
  </Item>
</Items>

Essentially, the XML query is supposed to return the title and the description for every Property which has an element called Language Code, which is equal to "en-us". I have a feeling that my problem lies in my XML code.

Upvotes: 0

Views: 1185

Answers (2)

shawty
shawty

Reputation: 5829

Try taking one of the dots out of

Dim itemProps = From p In element...<Property>

Your going 3 levels down, when you only need to go down 2.

If that doesn't work try just one dot, because essentially the path your travelling is only 1 below the root of the document.

Upvotes: 0

aquinas
aquinas

Reputation: 23796

This language code:

en_us

should be:

en-us

Upvotes: 2

Related Questions