Quering XElements for children with children attributes.

Posted by Arnej65 on Stack Overflow See other posts from Stack Overflow or by Arnej65
Published on 2010-05-11T19:50:33Z Indexed on 2010/05/11 19:54 UTC
Read the original article Hit count: 392

Filed under:
|

Here is the XML outline:

<Root> 
  <Thing att="11">    
    <Child lang="e">  
       <record></record>
       <record></record>
       <record></record>  
   </Child >
   <Child lang="f">  
       <record></record>  
       <record></record>                
       <record></record>
   </Child > 
 </Thing> 
</Root>

I have the following:

TextReader reader = new StreamReader(Assembly.GetExecutingAssembly()
                 .GetManifestResourceStream(FileName));

   var data = XElement.Load(reader);
foreach (XElement single in Data.Elements())
 {
      // english records
      var EnglishSet = (from e in single.Elements("Child")
         where e.Attribute("lang").Equals("e")
        select e.Value).FirstOrDefault();
}

But I'm getting back nothing. I want to be able to for Each "Thing" select the "Child" where the attribute "lang" equals a value.

I have also tried this, which has not worked.

var FrenchSet = single.Elements("Child")
.Where(y => y.Attribute("lang").Equals("f"))
.Select(x => x.Value).FirstOrDefault();

© Stack Overflow or respective owner

Related posts about linq-to-xml

Related posts about c#