How to display the XML File Data in ListBox using LINQ Query Concepts

By
Hi Friends ! Today ,I am going to explain "How to display the XML File Data in ListBox using LINQ Query Concepts".In this post i have used many concepts but How much you can use this concepts for your asp.net application,it is totally depends on you.You can perform write,Read ,update,delete and other operations also in XML File from here. You can use this whole concepts in your asp.net application.If You want to make a commercial application then you have to understand these concepts very carefully.In this application ,i will display two XML File data in List Box control which is placed in different location.I have used Linq concepts to fetch the specific data from XML File.Here My first XML File(student_data.xml) is placed inside our project(website)folder and second (myfile.xml) is placed inside my computer D: drives . You can placed it (XML file) in any drive but you will have to change the c# codes also. 
There are following steps to implement this concepts on  your asp.net web page as given below:-

Step 1 :- First open your visual studio -->file -->New -->Website -->Select AP.NET Empty website --> OK -->open Solution Explorer --> Add a web form(default.aspx) -->After that Drag and Drop List Box and Button controls on the page from toolbox as shown below:-

website
Step 2 :-Now Open solution Explorer --> Add a XML File(student_data.xml) -->and write the xml file's codes as given below:-
<?xml version="1.0" encoding="utf-8" ?>
<students>
  <data>
  <id>100</id>
  <name>Ram</name>
  <age>23</age>
  <company>TCS </company>
  </data>
  <data>
  <id>101</id>
  <name>Ramesh</name>
  <age>22</age>
  <company>Infosis</company>
  </data>
  <data>
  <id>102</id>
  <name>Rajesh</name>
  <age>26</age>
  <company>Wipro </company>
  </data>
  <data>
  <id>103</id>
  <name>kiran</name>
  <age>30</age>
  <company>HCL</company>
    </data>
  <data>
  <id>104</id>
  <name>Neha</name>
  <age>19</age>
  <company>TCS </company>
    </data>
  <data>
  <id>105</id>
  <name>Komal</name>
  <age>27</age>
  <company>IBM </company>
    </data>
  <data>
  <id>106</id>
  <name>Suneel</name>
  <age>21</age>
  <company>Wipro </company>
    </data>
  <data>
  <id>107</id>
  <name>Sachin</name>
  <age>25</age>
  <company>Mahindra Tech. </company>
    </data>
</students>
Step 3 :- Now Create second xml file(myfile.xml) in your computer " D://" drive --> First write the xml codes in Notepad as given below --> after that save Notepad codes as myfile.xml  extension in your D: drive-->done.
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Publisher>
<Serial>10000</Serial>
<title>The Wings of Fire</title>
<Author>APJ Abdul kalam</Author>
</Publisher>
<Publisher>
<Serial>10234</Serial>
<title>aptitude</title>
<Author>RS AGARWAL</Author>
</Publisher>
<Publisher>
<Serial>34567</Serial>
<title>English</title>
<Author>Dr.Ramesh kumar</Author>
</Publisher>
<Publisher>
<Serial>54365</Serial>
<title>c# Language</title>
<Author>Balaguruswamy</Author>
</Publisher>
</Books>
Step 4 :- Now Write the c# codes on each button's click (default.aspx.cs file) as given below:-
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        XDocument xmlDoc = XDocument.Load(Server.MapPath("~/student_data.xml"));
        var sid = from value1 in xmlDoc.Descendants("data")
       select (int)value1.Element("id");
        foreach (int i in sid)
        {
            ListBox1.Items.Add(i.ToString());
        }

        var names = from value2 in xmlDoc.Descendants("data")
                  select (string)value2.Element("name");
        foreach (string str1 in names)
        {
            ListBox2.Items.Add(str1);
        }
        var company = from value3 in xmlDoc.Descendants("data")
                    select (string)value3.Element("company");
        foreach (string str2 in company)
        {
            ListBox3.Items.Add(str2);
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        XDocument xmlDoc = XDocument.Load(@"D://myfile.xml");
        var Serial = from value1 in xmlDoc.Descendants("Publisher")
                  select (int)value1.Element("Serial");
        foreach (int i in Serial)
        {
            ListBox1.Items.Add(i.ToString());
        }

        var title = from value2 in xmlDoc.Descendants("Publisher")
                     select (string)value2.Element("title");
        foreach (string str1 in title)
        {
            ListBox2.Items.Add(str1);
        }
        var author = from value3 in xmlDoc.Descendants("Publisher")
                    select (string)value3.Element("Author");
        foreach (string str2 in author)
        {
            ListBox3.Items.Add(str2);
        }
       
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        ListBox1.Items.Clear();
        ListBox2.Items.Clear();
        ListBox3.Items.Clear();
    }
}

Description:-
  • First button's click ,i have loaded student_data.xml file data in main memory and display its data in ListBox control on the basis of LINQ query Concepts. 
  • Second button's click ,i have loaded myfile.xml file data from D: drive in main memory and display its data in ListBox control on the basis of LINQ query Concepts. 
  • Third button's click , I have cleared all ListBoxes values.
Step 5 :- Now Run the application (Press F5) --> and Press first button(Display the site's xml file data) --> You will see the following output as shown below:-
listbox1

Step 6 :-Now Press Clear Button --> After that press second button (Display the Drive's XML file Data) --> You will see the following output as shown below:-
listbox2

Note :- You can display the specific xml data file data with help of LINQ query concepts. Here i have displayed all xml file data without using any Linq query condition statements.You can use condition statements also with LINQ Query if required.  

I hope this is helpful for you.
Please help to share this application with your friends.
Download Whole Attached file
  Download

0 comments:

Post a comment

Powered by Blogger.