[ACCEPTED]-How to use a LINQ query to get XElement values when XElements have same name-linq-to-xml
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)
};
0
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>
<Record><Field>Value1_1</Field><Field>Value1_2</Field></Record>
<Record><Field>Value2_1</Field><Field>Value2_2</Field></Record>
</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)
Console.WriteLine(row);
Console.ReadKey();
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.
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.
Maybe this?
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Diagnostics;
[TestMethod]
public void Linq_XElement_Test()
{
string xml = @"<Table>
<Record>
<Field>Value1_1</Field>
<Field>Value1_2</Field>
</Record>
<Record>
<Field>Value2_1</Field>
<Field>Value2_2</Field>
</Record>
</Table>";
XElement elements = XElement.Parse(xml);
var qryRecords = from record in elements.Elements("Record")
select record;
foreach (var rec in qryRecords)
{
Debug.WriteLine(rec.Value);
}
var qryFields = from record in elements.Elements("Record")
from fields in record.Elements("Field")
select fields;
foreach (var fil in qryFields)
{
Debug.WriteLine(fil.Value);
}
IEnumerable<string> list = qryFields.Select(x => x.Value);
foreach (string item in list)
{
Debug.WriteLine(item);
}
}
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.
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");
ds.Tables.Add("Records");
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)
ds.Tables[0].Columns.Add();
DataRow row = ds.Tables[0].NewRow();
row.ItemArray = datum;
ds.Tables[0].Rows.Add(row);
}
}
Then return ds
and set the DataMember
to "Records"
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);
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);
}
}
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:
http://msdn.microsoft.com/en-us/library/bb308960.aspx
It explains it using a structurally 1 similar example to the one you presented.
Would an
IEnumerable<List<string>>
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;
x.Add(z.ToList());
}
return x.AsEnumerable();
Don't know if that works 1 for your specific scenario or not.
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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.