Saturday 13 October 2012

c sharp classes

HOW TO QUERY  XML USING LINQ


using System;
using System.Linq;
using System.Xml.Linq;


XElement books = XElement.Parse(
  @"<books>
      <book>
          <title>Pro LINQ: Language Integrated Query in C# 2010</title>
          <author>Joe Rattz</author>
      </book>
      <book>
          <title>Pro .NET 4.0 Parallel Programming in C#</title>
          <author>Adam Freeman</author>
      </book>
      <book>
          <title>Pro VB 2010 and the .NET 4.0 Platform</title>
          <author>Andrew Troelsen</author>
       </book>
    </books>");

var titles =
  from book in books.Elements("book")
  where (string) book.Element("author") == "Joe Rattz"
  select book.Element("title");

foreach(var title in titles)
  Console.WriteLine(title.Value);

No comments:

Post a Comment