Creating LINQ queries to access data from XML file.


In this article we are going to discuss XML in LINQ.We will see how we can create the LINQ queries for accessing the data from a XML file and then show the data in the gridview according t othe specific where clauses.We will see it with the help of an example.

We first create an XML file with the main node Nutrition and its sub nodes.
Below is the code of XML file.


<nutrition>

<food>
<name>Avocado Dip</name>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100"/>
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
</food>

<food>
<name>Bagels, New York Style </name>
<mfr>Thompson</mfr>
<serving units="g">104</serving>
<calories total="300" fat="35"/>
<total-fat>4</total-fat>
<saturated-fat>1</saturated-fat>
<cholesterol>0</cholesterol>
<sodium>510</sodium>
<carb>54</carb>
<fiber>3</fiber>
<protein>11</protein>
</food>

<food>
<name>Beef Frankfurter, Quarter Pound </name>
<mfr>Armitage</mfr>
<serving units="g">115</serving>
<calories total="370" fat="290"/>
<total-fat>32</total-fat>
<saturated-fat>15</saturated-fat>
<cholesterol>65</cholesterol>
<sodium>1100</sodium>
<carb>8</carb>
<fiber>0</fiber>
<protein>13</protein>
</food>

<food>
<name>Chicken Pot Pie</name>
<mfr>Lakeson</mfr>
<serving units="g">198</serving>
<calories total="410" fat="200"/>
<total-fat>22</total-fat>
<saturated-fat>9</saturated-fat>
<cholesterol>25</cholesterol>
<sodium>810</sodium>
<carb>42</carb>
<fiber>2</fiber>
<protein>10</protein>
</food>

</nutrition>




Now we will take a gridview and some buttons on whose clicks gridview will be populated with the specific data
according to the condition applied in queries.

Now in our page we will create diffrent types of queries using LINQ which will access this XML file
and acess the data.

1. Query to return total cholestrol level.

protected void btngetdata_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(Server.MapPath("nutrition.xml"));
var v = from x in doc.Element("nutrition").Elements()
select x;

int total=0;
foreach (XElement xe in v)
{
total += int.Parse(xe.Element("cholesterol").Value);
}

GridView1.DataSource = v;
GridView1.DataBind();

}


2. Query that returns the food name and cholestrol level.

protected void btngetdata_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(Server.MapPath("nutrition.xml"));
var v = from x in doc.Element("nutrition").Elements()
select new { FoodName = (string)x.Element("name"), ColesterolLevel = (int)x.Element("cholesterol") };

GridView1.DataSource = v;
GridView1.DataBind();
}


3. Query that returns the food that starts with C.

protected void btngetdata_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(Server.MapPath("nutrition.xml"));
var v = from x in doc.Element("nutrition").Elements()
where ((string)x.Element("name")).StartsWith("C")
//or another method as follows
// where x.element("name").value.StartsWith('C")
select new { Name = (string)x.Element("name") };
GridView1.DataSource = v;
GridView1.DataBind();
}


4. Query that returns the protein level and count numbe rof food.

protected void btngetdata_Click(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(Server.MapPath("nutrition.xml"));
var v = from x in doc.Element("nutrition").Elements()
group x by (int)x.Element("protein") into p
select new
{
ProteinLevel = p.Key,
TotalFoodItem = p.Count()

};

GridView1.DataSource = v;
GridView1.DataBind();

}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: