Microsoft Developer Network >
Forums Home
>
Archived Forums Forums
>
LINQ Project General
>
Read XML and Update DataTable
Read XML and Update DataTable
- I am trying to read from XML and update a DataTable. Simplified code given here, I can explain the whole scenario if needed. But I hope someone might already know what I am doing wrong. Here it is:
XElement myElement = XElement.Parse(myXmlString);
MyDataTable myTable = dataProvider.FindByID(myElement.Element("ID").Value);
myElement.Element("ID").ElementsAfterSelf().Select(eachElement => myTable.Rows[0][eachElement.Name.LocalName] = eachElement.Value);
Element names and Column names are same. Table has data. No errors.
When I 'watch' it upto ElementsAfterSelf() I can see it is working fine.
But I dont have a way to see what happens inside the Select().
The problem is the Columns in the DataTable are not getting updated with values from the XML Elements. Please help.
Thanks!
Answers
- Use a foreach loop e.g.
foreach (XElement el in myElement.Element("ID").ElementsAfterSelf())
{
myTable.Rows[0][el.Name.LocalName] = el.Value;
}
MVP XML My blog- Marked As Answer byDumboDotNet Friday, November 06, 2009 8:48 PM
- Theres nothing wrong... but I am just trying to use LINQ everywhere for fun.
I changed the code to use LINQ ForEach instead, like so:
myElement.Element("ID").ElementsAfterSelf().ToList().ForEach(eachElement => myTable.Rows[0][eachElement.Name.LocalName] = eachElement.Value);
Now that works... linqalicious... :-)
But slower... :-(
I wish I can directly Loop() thru any collection, IEnumerable...
Example: ElementsAfterSelf().ForEach(...)
Instead of: ElementsAfterSelf().ToList().ForEach(...)
Thanks for your feedback!- Marked As Answer byDumboDotNet Friday, November 06, 2009 8:48 PM
All Replies
- Use a foreach loop e.g.
foreach (XElement el in myElement.Element("ID").ElementsAfterSelf())
{
myTable.Rows[0][el.Name.LocalName] = el.Value;
}
MVP XML My blog- Marked As Answer byDumboDotNet Friday, November 06, 2009 8:48 PM
Yeah, I have the foreach and that works fine. Select() is not working.
Thanks anyways...
Using foreach() is not very Linqalicious... :-)
So I would like to get the Select() to work.
Any reason why Select() wont work?- You are constructing a query with LINQ. Unless you iterate over the query results the query is not executed. There is nothing wrong with using foreach with a LINQ query.
MVP XML My blog - Theres nothing wrong... but I am just trying to use LINQ everywhere for fun.
I changed the code to use LINQ ForEach instead, like so:
myElement.Element("ID").ElementsAfterSelf().ToList().ForEach(eachElement => myTable.Rows[0][eachElement.Name.LocalName] = eachElement.Value);
Now that works... linqalicious... :-)
But slower... :-(
I wish I can directly Loop() thru any collection, IEnumerable...
Example: ElementsAfterSelf().ForEach(...)
Instead of: ElementsAfterSelf().ToList().ForEach(...)
Thanks for your feedback!- Marked As Answer byDumboDotNet Friday, November 06, 2009 8:48 PM

