Ask a questionAsk a question
 

AnswerRead XML and Update DataTable

  • Friday, November 06, 2009 4:22 PMDumboDotNet Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Friday, November 06, 2009 4:26 PMMartin Honnen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
    •  
  • Friday, November 06, 2009 8:47 PMDumboDotNet Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

  • Friday, November 06, 2009 4:26 PMMartin Honnen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
    •  
  • Friday, November 06, 2009 6:41 PMDumboDotNet Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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?

  • Friday, November 06, 2009 6:51 PMMartin Honnen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Friday, November 06, 2009 8:47 PMDumboDotNet Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
    •