[ACCEPTED]-How to use a LINQ query to get XElement values when XElements have same name-linq-to-xml

Accepted answer
Score: 11

Would something like this help?

var a = from record in table.Elements("Record")
    select new
        one = (string)record.Elements().ElementAt(0),
        two = (string)record.Elements().ElementAt(1)


Score: 4

It sounds like you want to denormalize the 5 field so that it fits in 1 column in your 4 data grid.

Does the following help?

var table = XElement.Parse(@"<Table>

var temp = from record in table.Elements("Record")
           from field in record.Elements("Field")
           group field.Value by record into groupedFields
           select groupedFields.Aggregate((l, r) => l + ", " + r);

foreach (var row in temp)


Disclaimer: I 3 don't do much SQL or LINQ anymore, so this 2 probably could be made better. Feel free 1 to change it.

Score: 1

I don't get what the problem is and why 13 a number of these answers look so complicated 12 :-/ Here is my offering:

var r = (from record in table.Elements("Record")
         select (from element in record.Elements("Field")
                 select element.Value));

// => IEnumerable<IEnumerable<string>>

The inner IEnumerable 11 is for the columns and the outer is for 10 the rows. (The question is kind of confusing 9 because there is no IEnumerable<T,T'>, the above is my adaptation 8 of the intent.)

You could make the inner 7 IEnumerable into a string (e.g. Join on 6 ", "), but that's not very fun to put into 5 a grid if you mean the values as columns!

var r = (from record in table.Elements("Record")
         select String.Join(", ",
             record.Elements("Field").Select(f => f.Value).ToArray());

// => IEnumerable<string>

The 4 above could likely be made nicer if I actually 3 knew LINQ Expressions. However, it works 2 and covers the "other" case -- the "ToArray" is 1 for .NET 3.5 and below.

Score: 1

Maybe this?

using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Diagnostics;

    public void Linq_XElement_Test()

    string xml = @"<Table>

    XElement elements = XElement.Parse(xml);

    var qryRecords = from record in elements.Elements("Record")
                  select record;

    foreach (var rec in qryRecords)

    var qryFields = from record in elements.Elements("Record")
                 from fields in record.Elements("Field")
                 select fields;

    foreach (var fil in qryFields)

    IEnumerable<string> list = qryFields.Select(x => x.Value);

    foreach (string item in list)



Score: 0

You can chain calls to Elements():

var temp = from field in table.Elements("Record").Elements("Field")
           select field.Value;

You can also use 3 Descendants():

 var temp = from field in table.Descendants("Field")
            select field.Value;

However, this will return all <Field> elements 2 under <Table>, even if they are not within 1 a <Record> element.

Score: 0

I may be way off in what you're looking 2 for, but are you looking for something like 1 this?

        DataSet ds = new DataSet("Records DS");
        foreach (XElement record in table.Descendants("Record"))
            var temp = from r in record.Descendants("Field")
                       select r.Value;

            string[] datum = temp.ToArray();
            if (datum != null
                && datum.Length > 0)
                foreach (string s in datum)
                DataRow row = ds.Tables[0].NewRow();
                row.ItemArray = datum;

Then return ds and set the DataMember to "Records"

Score: 0

You need a 2-step processs:

Annotate the 3 records (in lieu of attributes) and then Enumerate 2 the fields and pass the values into an anonymous 1 class for binding, like so:

string xml = [string-goes-here];

  XElement elem = XElement.Parse(xml);

  int count= 0;
  foreach(XElement recordElems in elem.Elements("Record")){
    recordElems.SetAttributeValue("id", count);
  var temp = from record in elem.Elements("Record")
             from field in record.Elements("Field")
             select new { Record = "Record " + record.Attribute("id").Value, Field = field.Value };

  foreach (var item in temp)
    Console.WriteLine("{0}: {1}", item.Record, item.Field);
Score: 0

Er... I think you're using the wrong LINQ 5 paradigm for this problem. You should be 4 using LINQ to XML which is slightly different 3 maybe than you're expecting.

Check out this 2 link:


It explains it using a structurally 1 similar example to the one you presented.

Score: 0

Would an


work? (not sure how to get that 2 to display inline)

var recordList = from record in data.Elements("record") select record;
List<List<string>> x = new List<List<string>>(recordList.Count());
foreach (var record in recordList)
  var z = from field in record.Elements("field") select field.Value;
return x.AsEnumerable();

Don't know if that works 1 for your specific scenario or not.

Score: 0

var detail1 = from d in ds.tbl_Looking_Fors where 2 d.Profile_ID == id select 1 d.Looking_For ;

            string[] datum = detail1.ToArray();
            if (datum != null && datum.Length > 0)
                foreach (var row in datum)
                    Label6.Text = datum[0]+" , "+datum[1];


More Related questions