C# Reading multiple elements with same name using LINQ to XML

Posted by hs2d on Stack Overflow See other posts from Stack Overflow or by hs2d
Published on 2011-06-27T06:14:01Z Indexed on 2011/06/27 8:22 UTC
Read the original article Hit count: 160

Filed under:
|
|

Is there any better way doing this?

My xml:

<Template name="filename.txt">
  <Property name="recordSeparator">\r\n</Property>
  <Property name="fieldCount">16</Property>
</Template>

Linq:

            var property = from template in xml.Descendants("Template")
                       select new
                                  {
                                      recordDelim = template.Elements("Property").Where(prop => prop.Attribute("name").Value == "recordSeparator")
                                                    .Select(f => new { f.Value }),
                                      fieldCount = template.Elements("Property").Where(prop => prop.Attribute("name").Value == "fieldCount")
                                                    .Select(f => new { f.Value })
                                  };

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml