tags:

views:

46

answers:

2

Hi,

I have one xml file. That xml file have multiple elements. I want to read these elements and bind into datagrid using Linq in C#.

A: 

Why not to use DataSet.ReadXml() and then bind it into DataGrid?

Orsol
A: 

You will probably be better served using DataTable/DataSet for this binding. But if you want to use LINQ 2 XML you could do something like this...

var xml = XElement.Load("yourfile.xml");
var records = from element in xml.Elements()
              let col1 = element.Element("element1").Value
              let col2 = element.Element("element2").Value
              let col3 = element.Attribute("attribute1").Value
              select new {
                  col1,
                  col2
                  col3
              }

... note there are several spots where you could get NullReferenceExceptions in the above query.

Matthew Whited