If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Parsing XML using LINQ to XML

by Farooq Kaiser 5. February 2009 05:39
The following code will show how to parse XML using LINQ. 

<users>
 <user>
<id>001</id> 
<name>James</name> 
</user> 
<user>
<id>002</id> 
<name>Paul</name> 
</user>  
</users> 


   XDocument users = XDocument.Load(@"Users.xml");
         var query = from user in users.Elements("users").Elements("user")
         select user;
         foreach (XElement usr in query)
         {
              Console.WriteLine(usr.Element("id").Value);
         Console.WriteLine(usr.Element("name").Value); 
         }
 

Currently rated 3.5 by 2 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

.NET | .NET | C# | C# | XML | XML

Jobs Autos Real estate Videos Power by Google