SIGN UP MEMBER LOGIN:    
ARTICLE

Reading XML Attributes using LINQ to XML in Silverlight

Posted by Mahesh Chand Articles | LINQ with C# November 29, 2010
This code snippet demonstrates how to load an XML file and reads its nodes and their attributes in a Silverlight application using LINQ to XML.
Reader Level:

We are building a Website in Silverlight that needs to access some data from a XML file.

Here is an example of XML document.


<?xml version="1.0" encoding="utf-8" ?>

<Contents>
  <
Content Title="Mahesh Chand"
         
ImageUri="ADO.NET Programming with C#"
         
RedirectText="APress"
         
RedirectUri="2003"
         
Description="44.95" > 
 
</
Content> 

  <Content Title="Raj Beniwal"
         
ImageUri="Mastering Silverlight"
         
RedirectText="Mindcracker"
         
RedirectUri="2010"
         
Description="49.95" />

  <Content Title="Mike Gold"
         
ImageUri="Visual C# Programming"
         
RedirectText="Microgold Press"
         
RedirectUri="2005"
         
Description="44.95" /> 

</Contents>

 

Before you can use LINQ to XML, you must add a reference to the System.Xml.Linq.dll assembly and import the following namespace in the code.

using System.Xml.Linq;

 

The following code snippet loads XML file in a Silverlight application using LINQ to XML and reads all of the attributes of the nodes.  To run this code, create a Silverlight Web application, add a Button and a ListBox control and write the following code on the Button click event handler.

// Load XML document
XDocument
doc = XDocument.Load("Authors.xml");

// Get all nodes of the root node

IEnumerable<XNode> nodes =
    from xmlNode in doc.Root.Nodes()
    select xmlNode;

        

foreach (XNode node in nodes)
{          

    XElement elm = (XElement)node;

    IEnumerable<XAttribute> attList =
    from at in elm.Attributes()
    select at;

    foreach (XAttribute att in attList)
    {

        listBox1.Items.Add(att.Name + " : " + att.Value);
    }

}

         

 

Login to add your contents and source code to this article
share this article :
post comment
 

This is a very good article! Thanks for the great work. Could you show an example on using the code with a xml rss feed where the xml is in the same general format as this example. <Contents> <Content Title="Mahesh Chand" ImageUri="ADO.NET Programming with C#" RedirectText="APress" RedirectUri="2003" Description="44.95" > : : : Thanks

Posted by R M Mar 10, 2011

Thanks Mamta. Appreciate your comment.

Posted by Mahesh Chand Dec 07, 2010

This was something that many books or articles dont cover. So it was very good that you wrote this stuff here. Thank you for putting this up.

Posted by Mamta M Dec 07, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor