Wednesday, 27 April 2016

Reading XML Document in C# Using Linq With Where Clause In Asp.Net

4/27/2016 - By Pranav Singh 0

This article will show you how you can read xml file using linq to C# with linq where clause in asp.net.
So for this article first we will create a new asp.net application and add an xml file.

<?xml version="1.0" encoding="utf-8" ?>
<countryName>
  <country>
    <id>1</id>
    <name>India</name>
  </country>
  <country>
    <id>2</id>
    <name>Nepal</name>
  </country>
  <country>
    <id>3</id>
    <name>Pakistan</name>
  </country>
  <country>
    <id>4</id>
    <name>Sri Lanka</name>
  </country>
</countryName>

After this we will add a new page and add the below code into the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Reading XML Document in C# Using Linq With Where Clause In Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
        <h3>Search For "India"</h3>
        <div>
            Id:
            <asp:Label ID="lblId" runat="server" Text="Label" style="font-weight: 700"></asp:Label><br />
            Name:<asp:Label ID="lblName" runat="server" Text="Label" style="font-weight: 700"></asp:Label>
        </div>
    </form>
</body>
</html>

Now check the .cs page code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //reading xml document in c# using linq
        protected void Page_Load(object sender, EventArgs e)
        {
            XDocument xmldocument = XDocument.Load(Server.MapPath("XMLFile1.xml"));
            var countlryList = (from data in xmldocument.Descendants("country")
                                select new
                                {
                                    id = data.Element("id").Value,
                                    name = data.Element("name").Value
                                }).Where(m => m.id == "1");

            if (countlryList != null)
            {
                lblId.Text = countlryList.First().id;
                lblName.Text = countlryList.First().name;
            }
        }
    }
}

In above code I have read the xml and then applied filter to the collection for getting the specific record using linq.

Now we have done, run the application to check the output.


Tags: , , , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top