USING XDOCUMENT & LINQ COMBINATION


I have below XML

<?xml version="1.0" encoding="utf-8" ?>
<products>
    <product>
        <code>1</code>
        <name>Product1</name>
        <description>1st product</description>
        <price>100</price>
    </product>
    <product>
        <code>2</code>
        <name>Product2</name>
        <description>2nd product</description>
        <price>200</price>
    </product>
    <product>
        <code>3</code>
        <name>Product3</name>
        <description>3rd product</description>
        <price>800</price>
    </product>
</products>

I also have one datagrid inside my page. My HTML as below

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    </form>
</body>
</html>

Look at my code behind 

 protected void Page_Load(object sender, EventArgs e)
    {
        XDocument prodDoc = XDocument.Load(Server.MapPath("Products.xml"));
        var products = from prd in prodDoc.Descendants("product") select new { name = (string)prd.Element("name"), desc = (string)prd.Element("description"), price = (int)prd.Element("price") };
        GridView1.DataSource = products;
        GridView1.DataBind();
    }

1st line I am using XDocument to load the existing XML document. XDocument is really flexible with LINQ.

2nd line I used a simle LINQ. It's saying "I want all elements named "product" and store those to a variable named 'prd'. Then from that 'prd' variable I need to select 2 fields named 'name','desc' and 'price. 'name' field should be loaded with 'name' element value and 'desc' should be loaded with 'description' element value and 'price' field should be loaded with 'price' element value." I think you got this query explanation.

3rd and 4th line just binding my LINQ out put to the data grid. You should try with various combinations of XDocumment + LINQ like this.

Before ending 1 alert. There is always better chance to get runtime errors during XML parsing. Especially when there may not be 1 element available. For eg. I just modified the xml like below. Commented one price.
<?xml version="1.0" encoding="utf-8" ?>
          ------------------------
          ------------------------
        <!--<price>800</price>-->
    </product>
</products>

Once you run the alicationyou will get error as we are trying to convert one null value inside LINQ query ie.

price = (int)prd.Element("price").

The resolution is the for all such xml parsing use nullable objets in place. So convert your LINQ query like below.

 var products = from prd in prodDoc.Descendants("product") select new { name = (string)prd.Element("name"), desc = (string)prd.Element("description"), price = (int?)prd.Element("price") };

 New key word "?" will make specifc element value as nullable.
Ebook Download
View all
Learn
View all