What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 20466 |  Welcome, Guest!   Register  Login
 Home > Blogs > LINQ > How to read attribute based XML file using LINQ? ...
SheoNarayan

How to read attribute based XML file using LINQ?

 Blog author: SheoNarayan | Posted on: 4/9/2011 | Category: LINQ Blogs | Views: 3623 | Status: [Microsoft_MVP] [Administrator] | Points: 75 | Alert Moderator   


In this small blog post I am going to show how to read attribute based XML File and store into the collection object.

My xml file (XmlData.xml) looks like below
 
<?xml version="1.0" encoding="utf-8" ?>
<Departments>
  <Department Name="HR" Id="1"/>
  <Department Name="Finance" Id="2"/>
  <Department Name="Software Engineering" Id="3"/>
  <Department Name="Quality Assurance" Id="4"/>
  <Department Name="Manufacturing" Id="5"/>
  <Department Name="Sales" Id="6"/>
</Departments>
GridView in which data shall be populated

<asp:GridView ID="GridView1" runat="server" EnableViewState="false" />
Code behind to read the attribute based XML file (XmlData.xml file). Notice that you will need to use System.Xml.Linq namespace to work with following code snippet.

string xmlFile = Server.MapPath("~/XmlData.xml");
        XDocument doc = XDocument.Load(xmlFile);
        var data = from d in doc.Descendants("Department")
                   where d.HasAttributes
                   select new
                   {
                       Name = d.Attribute("Name").Value,
                       Id = d.Attribute("Id").Value
                   };

        GridView1.DataSource = data;
        GridView1.DataBind();
Notice that if your XML has attribute, they will not be by default converted as columns or properties by LINQ so you need to explicitly retrieve the attributes and set as virtual property names of the returned object (notice the Select block into the LINQ statement).

The output of would look something like

NameId
HR1
Finance2
Software ngineering3
Quality Assurance4
Manufacturing5
Sales6

So easy ! Hope this help.



Regards,
Sheo Narayan, Microsoft MVP
The Founder
http://www.dotnetfunda.com
Found interesting? Add this to:


About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

 Responses

Akiii
Posted by: Akiii | Posted on: 4/16/2011 | Level: Bronze | Status: [Member] | Points: 15 | Alert Moderator 

Thank you sir......
it helped me to solve a problem......
good article

Akiii

>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 4:36:18 AM