24 May 2011

LINQ to XML: XML Integration

LINQ to XML: XML Integration


-.NET Language-Integrated Query for XML (LINQ to XML) allows XML data to be queried by using the standard query operators as well
as tree-specific operators that provide
XPath-like navigation through descendants, ancestors, and siblings.

-It provides an efficient in-memory representation for XML that integrates with the existing System.Xml reader/writer
infrastructure and is easier to use than W3C DOM.
There are three types that do most of the work of integrating XML with queries: XName, XElement and XAttribute.

XName provides an easy-to-use way to deal with the namespace-qualified identifiers (QNames) used as both element and attribute names.
XName handles the efficient atomization of identifiers transparently and allows either symbols or plain strings to be used wherever
a QName is needed.

XML elements and attributes are represented using XElement and XAttribute respectively.
XElement and XAttribute support normal construction syntax, allowing developers to write XML expressions using a natural syntax:

var e = new XElement("Person",
new XAttribute("CanCode", true),
new XElement("Name", "Loren David"),
new XElement("Age", 31));

var s = e.ToString();

This corresponds to the following XML:


Loren David
31


XML elements can also be constructed from an existing XmlReader or from a string literal:

var e2 = XElement.Load(xmlReader);
var e1 = XElement.Parse(
@"
Loren David
31
");


XElement dovetails with the query operators, allowing developers to write queries against non-XML information and produce XML results by constructing XElements in the body of a select clause:

var query = from p in people
where p.CanCode
select new XElement("Person",
new XAttribute("Age", p.Age),
p.Name);

1 comment:

  1. http://msdn.microsoft.com/en-us/library/bb387061(v=VS.90).aspx

    ReplyDelete

Comments Welcome