This
article will show you can user linq to xml using c#. In this I have shown how
you can read xml and apply filter using linq by c#.
So for this article first we will prepare an XML file.
So for this article first we will prepare an XML file.
<menu>
<menuitem Text="Main Category" Id='1'></menuitem>
<menuitem Text="Sub Category" Id='2'></menuitem>
<menuitem Text="Sub Sub Category" Id='3'></menuitem>
</menu>
|
After this please check the code.
string xmlFileName = "XMLFile.xml";
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(xmlFileName));
//Now we will find value for Id 1 bypplying linq query
with where clause
var filterData =
ds.Tables[0].AsEnumerable().Where(m => m.Field<string>("Id") == "1");
|
In above code I have first read the xml file and then apply the filter. You must check one thing in where clause, in m.Field datatype I have taken string. If you take int it will throw casting error.
Now lets
check the dataset values.
0 comments:
Please let me know your view