This is nothing like that. I think what your trying to do is bind against some data in the xml file. When you bind the GridView to a list of XElement without creating a proper CLR type for the column names you will get columns that are the properties of
the XElement type. If you have nothing against datasets it would be simpler to do something like this:
DataSet ds = new DataSet();
ds.ReadXml("Simple.xml");
GridView1.DataSource = ds;
GridView1.DataBind();
Reading the xml into a dataset creates a structure that can be understood by the gridview.
Alternatively you can do something more complex. In asp.net 3.5 we introduced an IAutoFieldGenerator interface that I explain
here in my blog.
I'm not sure which solution you are looking for or what constrains you have for your application but this is what the other code would look like:
public class XElementColumnGenerator : IAutoFieldGenerator {
private DataSet _dataSet;
public XElementColumnGenerator(string xmlFile) {
if (xmlFile == null) {
throw new ArgumentNullException("xmlFile");
}
// Read the schema from the DataSet
_dataSet = new DataSet();
_dataSet.ReadXmlSchema(xmlFile);
}
private IEnumerable<string> GetColumnNames() {
return _dataSet.Tables[0].Columns
.OfType<DataColumn>()
.Select(c => c.ColumnName);
}
public ICollection GenerateFields(Control control) {
return (from c in GetColumnNames()
select new XElementBoundField {
HeaderText = c,
DataField = c
}).ToArray();
}
}
public class XElementBoundField : BoundField {
protected override object GetValue(Control controlContainer) {
XElement rootElement = (XElement)DataBinder.GetDataItem(controlContainer);
XElement valueElement = rootElement.Element(DataField);
return valueElement != null ? valueElement.Value : null;
}
}
And to use this...
protected void Page_Load(object sender, EventArgs e) {
string xmlFile = Server.MapPath("~/Data.xml");
XDocument document = XDocument.Load(xmlFile);
var booksQuery = from books in document.Elements("NewDataSet").Elements("Table")
select books;
GridView1.ColumnsGenerator = new XElementColumnGenerator(xmlFile);
GridView1.DataSource = booksQuery;
GridView1.DataBind();
}
Actually i read the data from xml file minimum it have 2 lakhs of records the same N. number of xml files , while clicking Search button based on user input i want to read the exact data alone from that xml files (not read all data to dataset if i'm doing
like this it will take too time and definately performance should affect)and keep in a datatable object finally display into gridview.
This is what i expect. Now i tried your code i create a seperate class and added your code but it raise an error at
Error 1 'System.Data.DataColumnCollection' does not contain a definition for 'OfTypeDataColumn' and no extension method 'OfTypeDataColumn' accepting a first argument of type 'System.Data.DataColumnCollection' could be found (are you missing a using directive
or an assembly reference?)
Could you please tell me how to solve this error?
And i think now you understand my task, Could you please tell me is it possible to write Dynamic Linq query to XML file? And tell me for my situation am going right way or not?
Any doubts please feel free to ask me. If this post is answer of your question then don't forgot to Click "Mark As Answer".
J.Jeyaseelan
GridView1.ColumnsGenerator = new XElementColumnGenerator(xmlFile);
GridView1.DataSource = booksQuery;
GridView1.DataBind();
Error 1 The type or namespace name 'XElementColumnGenerator' could not be found (are you missing a using directive or an assembly reference?)
If i use the another class then it raise
Error 1 'System.Data.DataColumnCollection' does not contain a definition for 'OfTypeDataColumn' and no extension method 'OfTypeDataColumn' accepting a first argument of type 'System.Data.DataColumnCollection' could be found (are you missing a using directive
or an assembly reference?) error at the same place i already mentioned.
could you pelase tell me to solve this problem?
And also tell me how to write dynamic query to xml, i think you people knew how to write the query please tell me, long days i'm trying to solve the problem.
Any doubts please feel free to ask me. If this post is answer of your question then don't forgot to Click "Mark As Answer".
J.Jeyaseelan
Error 1 The type or namespace name 'XElementColumnGenerator' could not be found (are you missing a using directive or an assembly reference?)
You need to include the class..
jeyaseelan@ajsquare.net
Error 1 'System.Data.DataColumnCollection' does not contain a definition for 'OfTypeDataColumn' and no extension method 'OfTypeDataColumn' accepting a first argument of type 'System.Data.DataColumnCollection' could be found (are you missing a using directive
or an assembly reference?) error at the same place i already mentioned.
I said in my earlier reply that it was a type and the correct method is OfType not OfTypeDataColumn.
Yahoo.......Davidfowl this type of code also suit for me. Its really superb code
One more question i want to convert the data into DataTable i tried with CopyToDataTable then it seems
HasAttributes
HasElements
IsEmpty
Value
Uncheckbox
CheckBox
Uncheckbox
1mary23
Uncheckbox
CheckBox
Uncheckbox
2joseph34
Uncheckbox
CheckBox
Uncheckbox
3john44
Uncheckbox
CheckBox
Uncheckbox
4yova23
Uncheckbox
CheckBox
Uncheckbox
5yehova32
Uncheckbox
CheckBox
Uncheckbox
6mary23
Uncheckbox
CheckBox
Uncheckbox
7joseph34
Uncheckbox
CheckBox
Uncheckbox
8john44
Uncheckbox
CheckBox
Uncheckbox
9yova23
i expect this for further processing could you please tell me how to do this?
and here they are explain about static query,
http://weblogs.asp.net/scottgu/archive/2007/08/07/using-linq-to-xml-and-how-to-build-a-custom-rss-feed-reader-with-it.aspx but i want to know dynamic linq query to xml.
Thanks in advance
Any doubts please feel free to ask me. If this post is answer of your question then don't forgot to Click "Mark As Answer".
J.Jeyaseelan
Actually i read some records from xml file dynamically then i want to bind those records into gridview then those records should be access for some other calculations(those are already written code ) so what i want is convert the records into Datatable then
i just pass this Datatable to the existing code. If its over then my work also over.
Could you please help me to solve this problem?
Any doubts please feel free to ask me. If this post is answer of your question then don't forgot to Click "Mark As Answer".
J.Jeyaseelan
XElement setup = (from p in x.Descendants() select p).First(); foreach (XElement xe in setup.Descendants())
// build your DataTable dt.Columns.Add(new DataColumn(xe.Name.ToString(),
typeof(string)));
// add columns to your dt
var all = from p in x.Descendants(setup.Name.ToString()) select p; foreach (XElement xe in all)
{
DataRow dr = dt.NewRow(); foreach (XElement xe2 in xe.Descendants())
dr[xe2.Name.ToString()] = xe2.Value; //add in the values dt.Rows.Add(dr);
} return dt;
}
</div> <div class=code>This is convert the records into DataTable but in my case i have lakhs/Millions of records if all the records should be populated after every for loop then its take too much of time.</div> <div class=code>Could you please tell me is there
anyway to copy into DatTable without this much oof loops. </div>
Any doubts please feel free to ask me. If this post is answer of your question then don't forgot to Click "Mark As Answer".
J.Jeyaseelan
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: bind records to datagridview using LINQ?
Jan 29, 2009 07:34 AM|LINK
This is nothing like that. I think what your trying to do is bind against some data in the xml file. When you bind the GridView to a list of XElement without creating a proper CLR type for the column names you will get columns that are the properties of the XElement type. If you have nothing against datasets it would be simpler to do something like this:
DataSet ds = new DataSet();
ds.ReadXml("Simple.xml");
GridView1.DataSource = ds;
GridView1.DataBind();
Reading the xml into a dataset creates a structure that can be understood by the gridview.
Alternatively you can do something more complex. In asp.net 3.5 we introduced an IAutoFieldGenerator interface that I explain here in my blog.
I'm not sure which solution you are looking for or what constrains you have for your application but this is what the other code would look like:
public class XElementColumnGenerator : IAutoFieldGenerator { private DataSet _dataSet; public XElementColumnGenerator(string xmlFile) { if (xmlFile == null) { throw new ArgumentNullException("xmlFile"); } // Read the schema from the DataSet _dataSet = new DataSet(); _dataSet.ReadXmlSchema(xmlFile); } private IEnumerable<string> GetColumnNames() { return _dataSet.Tables[0].Columns.OfType<DataColumn>()
.Select(c => c.ColumnName); } public ICollection GenerateFields(Control control) { return (from c in GetColumnNames() select new XElementBoundField { HeaderText = c, DataField = c }).ToArray(); } } public class XElementBoundField : BoundField { protected override object GetValue(Control controlContainer) { XElement rootElement = (XElement)DataBinder.GetDataItem(controlContainer); XElement valueElement = rootElement.Element(DataField); return valueElement != null ? valueElement.Value : null; } }
And to use this...
Hope this helps.Senior SDE, ASP.NET Team, Microsoft
jeyaseelan@a...
Contributor
5124 Points
2025 Posts
Re: bind records to datagridview using LINQ?
Jan 29, 2009 08:35 AM|LINK
Davidfowl thanks for your information
Actually i read the data from xml file minimum it have 2 lakhs of records the same N. number of xml files , while clicking Search button based on user input i want to read the exact data alone from that xml files (not read all data to dataset if i'm doing like this it will take too time and definately performance should affect)and keep in a datatable object finally display into gridview.
This is what i expect. Now i tried your code i create a seperate class and added your code but it raise an error at
private IEnumerable<string> GetColumnNames(){
return _dataSet.Tables[0].Columns.OfTypeDataColumn<DataColumn>().Select(c => c.ColumnName);}
error message is
Error 1 'System.Data.DataColumnCollection' does not contain a definition for 'OfTypeDataColumn' and no extension method 'OfTypeDataColumn' accepting a first argument of type 'System.Data.DataColumnCollection' could be found (are you missing a using directive or an assembly reference?)
Could you please tell me how to solve this error?
And i think now you understand my task, Could you please tell me is it possible to write Dynamic Linq query to XML file? And tell me for my situation am going right way or not?
J.Jeyaseelan
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: bind records to datagridview using LINQ?
Jan 29, 2009 08:57 AM|LINK
Try both ways and see which is easier for you.
OfTypeDataColumn is a type its just OfType.
Linq to XML is definately easier to query.
Senior SDE, ASP.NET Team, Microsoft
jeyaseelan@a...
Contributor
5124 Points
2025 Posts
Re: bind records to datagridview using LINQ?
Jan 29, 2009 09:37 AM|LINK
Davidfowl thanks for your response.
public
class XElementBoundField : BoundField{
protected override object GetValue(Control controlContainer){
XElement rootElement = (XElement)DataBinder.GetDataItem(controlContainer); XElement valueElement = rootElement.Element(DataField); return valueElement != null ? valueElement.Value : null;}
}
if i use this method then error raise at
GridView1.ColumnsGenerator = new XElementColumnGenerator(xmlFile);GridView1.DataSource = booksQuery;
GridView1.DataBind();
Error 1 The type or namespace name 'XElementColumnGenerator' could not be found (are you missing a using directive or an assembly reference?)
If i use the another class then it raise
Error 1 'System.Data.DataColumnCollection' does not contain a definition for 'OfTypeDataColumn' and no extension method 'OfTypeDataColumn' accepting a first argument of type 'System.Data.DataColumnCollection' could be found (are you missing a using directive or an assembly reference?) error at the same place i already mentioned.
could you pelase tell me to solve this problem?
And also tell me how to write dynamic query to xml, i think you people knew how to write the query please tell me, long days i'm trying to solve the problem.
J.Jeyaseelan
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: bind records to datagridview using LINQ?
Jan 29, 2009 09:42 AM|LINK
You need both classes for the code to compile.
You need to include the class..
I said in my earlier reply that it was a type and the correct method is OfType not OfTypeDataColumn.
Querying using Linq to XML
http://weblogs.asp.net/scottgu/archive/2007/08/07/using-linq-to-xml-and-how-to-build-a-custom-rss-feed-reader-with-it.aspx
Senior SDE, ASP.NET Team, Microsoft
jeyaseelan@a...
Contributor
5124 Points
2025 Posts
Re: bind records to datagridview using LINQ?
Jan 29, 2009 10:11 AM|LINK
Yahoo.......Davidfowl this type of code also suit for me. Its really superb code
One more question i want to convert the data into DataTable i tried with CopyToDataTable then it seemsHasAttributes
i expect this for further processing could you please tell me how to do this?
and here they are explain about static query,
http://weblogs.asp.net/scottgu/archive/2007/08/07/using-linq-to-xml-and-how-to-build-a-custom-rss-feed-reader-with-it.aspx but i want to know dynamic linq query to xml.
Thanks in advance
J.Jeyaseelan
jeyaseelan@a...
Contributor
5124 Points
2025 Posts
Re: bind records to datagridview using LINQ?
Jan 29, 2009 12:00 PM|LINK
Davidfowl could you please answer me
J.Jeyaseelan
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: bind records to datagridview using LINQ?
Jan 30, 2009 04:45 AM|LINK
Why do you want to copy it to a data table?
I'm not sure what kind of code you want to write.
Senior SDE, ASP.NET Team, Microsoft
jeyaseelan@a...
Contributor
5124 Points
2025 Posts
Re: bind records to datagridview using LINQ?
Feb 03, 2009 01:54 AM|LINK
Davidfowl sorry for late reply,
Actually i read some records from xml file dynamically then i want to bind those records into gridview then those records should be access for some other calculations(those are already written code ) so what i want is convert the records into Datatable then i just pass this Datatable to the existing code. If its over then my work also over.
Could you please help me to solve this problem?
J.Jeyaseelan
jeyaseelan@a...
Contributor
5124 Points
2025 Posts
Re: bind records to datagridview using LINQ?
Feb 03, 2009 03:16 AM|LINK
Davidfowl, thanks for your responses.
Now i have one code
<div class=code>public DataTable XElementToDataTable(XElement x){
DataTable dt = new DataTable();
XElement setup = (from p in x.Descendants() select p).First();
foreach (XElement xe in setup.Descendants()) // build your DataTable
dt.Columns.Add(new DataColumn(xe.Name.ToString(), typeof(string))); // add columns to your dt
var all = from p in x.Descendants(setup.Name.ToString()) select p;
foreach (XElement xe in all)
{
DataRow dr = dt.NewRow();
foreach (XElement xe2 in xe.Descendants())
dr[xe2.Name.ToString()] = xe2.Value; //add in the values
dt.Rows.Add(dr);
}
return dt;
}
</div> <div class=code>This is convert the records into DataTable but in my case i have lakhs/Millions of records if all the records should be populated after every for loop then its take too much of time.</div> <div class=code>Could you please tell me is there anyway to copy into DatTable without this much oof loops. </div>
J.Jeyaseelan