Linq to xml

1,091 views

Published on

.net ; LINQ ; XML

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,091
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
24
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Linq to xml

  1. 1. LINQ Language Integrated Query (LINQ, pronounced "link") isa Microsoft .NET Framework component that adds nativedata querying capabilities to .NET languages, although ports existfor Java, PHP and JavaScript. LINQ defines a set of method names (called standard queryoperators, or standard sequence operators), along with translationrules from so-called query expressions to expressions using thesemethod names, lambda expressions and anonymous types. Thesecan, for example, be used to project and filter data into arrays,enumerable classes, XML (LINQ to XML), relational databases, andthird party data sources.
  2. 2. ADVANTAGESQuick turn around for developmentQueries can be dynamically runTables are automatically created into classColumns are automatically created into propertiesRelationship are automatically applied to classesLambda expressions are awesomeData is easy to setup and use
  3. 3. DISADVANTAGESNo clear outline for TiersNo good way of view permissionsSmall data sets will take longer to build the query thanexecuteThere is an overhead for creating queriesWhen queries are moved from sql to application side,joins are very slowDBMS concurrency issuesHard to understand advance queries usingExpressions
  4. 4. LINQ ProvidersThe C# 3.0 specification defines a so-called Query Expression Pattern along withtranslation rules from a LINQ expression to anexpression in a subset of C# 3.0 without LINQexpressions.
  5. 5. These different providers define the different flavorsof LINQ: LINQ to Objects: The LINQ to Objects provider is used for in-memory collections, using the local query execution engine of LINQ. LINQ to XML (formerly called XLINQ): The LINQ to XML provider converts an XML document to a collection of XElement objects, which are then queried against using the local execution engine that is provided as a part of the implementation of the standard query operator.
  6. 6. LINQ to SQL (formerly called DLINQ): The LINQ to SQL providerallows LINQ to be used to query SQL Server databases,including SQL Server Compact databases. Since SQL Server datamay reside on a remote server, and because SQL Server has its ownquery engine, LINQ to SQL does not use the query engine of LINQ.LINQ to DataSets: The LINQ to SQL provider works onlywith Microsoft SQL Server databases; to support any genericdatabase, LINQ also includes the LINQ to DataSets, which usesADO.NET to handle the communication with the database. Once thedata is in ADO.NET Datasets, LINQ to DataSets execute queriesagainst these datasets.
  7. 7. What is LINQ to XML?Working with XML using Microsofts .NET Framework version 2.0 andbelow is a cumbersome task.The API available follows the W3C DOM model, and is document-centric. Everything begins with the document; you cant create elementswithout having a document; even a fragment of XML is a document.In the latest release of the .NET Framework, however, this haschanged. XML is now element-centric. With features like object initialization and anonymous types, its veryeasy to create XML. Add to this the features of LINQ, and we now have avery easy to use and powerful tool for XML.
  8. 8. What is LINQ to XML?LINQ to XML is a LINQ-enabled, in-memory XML programminginterface that enables you to work with XML from within the .NETFramework programming languages.LINQ to XML is like the Document Object Model (DOM) in that itbrings the XML document into memory. You can query and modifythe document, and after you modify it you can save it to a file orserialize it and send it over the Internet. However, LINQ to XMLdiffers from DOM: It provides a new object model that is lighter weightand easier to work with, and that takes advantage of languageimprovements in Visual C# 2008.
  9. 9. The most important advantage of LINQ to XML is its integration withLanguage-Integrated Query (LINQ). This integration enables to writequeries on the in-memory XML document to retrieve collections ofelements and attributes. The query capability of LINQ to XML iscomparable in functionality (although not in syntax) to XPath and XQuery.The integration of LINQ in Visual C# 2008 provides stronger typing,compile-time checking, and improved debugger support.All LINQ to XML types are defined in the System.Xml.Linq namespace.LINQ to XML comprises of an XML DOM, which we call the X-DOM
  10. 10. X-DOM OverviewXObject is the root of the inheritance hierarchy;XElement and XDocument are roots of the containership hierarchy.
  11. 11. The System.Xml.Linq NamespaceLINQ to XML allows querying XML data. A new namespace in .NET3.5, System.Xml.Linq, contains classes for creating and querying XMLtrees: Xname: Represents a name of an XML element or attribute. XNode represents the abstract concept of a node in the XML tree. XContainer, derived from XNode, is an abstract base class for all nodes that can have child nodes ;is the base class for XDocument and XElement. XDocument represents an XML document, that contains the root level XML constructs, such as: a document type declaration (XDocumentType), one root element (XElement), zero or more comments objects (XComment).
  12. 12.  XElement is the fundamental type for constructing XML data; it has an XName, optionally one or more attributes, and can also have content. An element is actually a container (derived from XContainer), that contains other XML nodes (XNode), such as XComment, XProcessingInstruction or XText. XAttribute represents an XML attribute; Attributes are not nodes in the XML tree, thus not derived from XNode. XComment, used for comments to the root node or as children of an element.
  13. 13. Constructing an XML document using LINQThe LINQ to XML object model does provides XDocuments and XElement classes toconstruct XML Documents.Step 1:XElement salesorder= new XElement("SALESORDER", new XElement("SOHEADER", new XElement("ORDERID",123), new XElement("CUSTOMERNAME","ABC")),new XElement("SOLINE",new XElement("ITEMNAME","2006")));Step 2:XDocument doc= new XDocument(new XDeclaration("1.0","utf-8",""),new XComment("LINQ to XML salesorder"), salesorder);Step 3:doc.Save(“XMLFile.xml");
  14. 14. Output of XMLFile.xml<?xml version="1.0" encoding="utf-8"?><!--LINQ to XML salesorder--><SALESORDER> <SOHEADER> <ORDERID>123</ORDERID> <CUSTOMERNAME>ABC</CUSTOMERNAME> </SOHEADER> <SOLINE> <ITEMNAME>2006</ITEMNAME> </SOLINE></SALESORDER>Constructing a document in this way is possible because of the functionalconstruction feature in LINQ to XML. Functional construction is simply a means ofcreating an entire document tree in a single statement.
  15. 15. Load an XML documentWe can load existing XML file into a LINQ in XML tree so that we canread it, manupulate it.XElement salesorder = XElement.Load(@”C:/so.xml”);XElement.load method is used to load the xml into XElement from anyfile or textreader.
  16. 16. Traversing XML LINQ to XML provides method for getting the children of an XElement. Nodes() method is used to get all the children of an XElement( or an XDocument). foreach(c in salesorder.Nodes()) { Console.writeLine( c ); }
  17. 17. Inserting XML in LINQWe can easily add element to the LINQ to XML object modeleasily by using Add() method.Salesorder.Add(new Element(“SOLINES”, new Element(“ITEMNAME”,2009));
  18. 18. Deleting XML element in LINQTo delete specific Xelement, we need to navigate to the contents todelete and call the Remove() method.Salesorder.Element(“SOLINES”).Remove();
  19. 19. Updating XML in LINQTo update XML, navigate to Xelement whose contents is to bereplaced and then use the ReplaceNode() method.Xelement header=salesorder.Element(“SOHEADER”);Xelement orderid=header.Element(“ORDERID”);Orderid.ReplaceNodes(“456”);
  20. 20. Thank You

×