Linq To XML Overview
Dale Hawthorne
For Sherwin Williams
Linq To XML Overview -2
Previous .NET Options:
• XmlDocument: Implements DOM, processor
and memory intensive due to recursion,
loading entire document in a tree before
proceeding
• XmlReader, XmlWriter, XslCompiledTransform,
XmlNamespaceManager: Fast but code
implementations are difficult to understand,
not very readable
Linq To XML Overview -3
Advantages of Linq To XML:
• Built over XMLWriters and XMLReaders, will
generally be faster than XmlDocument class
• Uses Linq query syntax, so processing XML more
like asking or answering a question than parsing a
document
• Works with existing XML Schemas (.XSD), XML
Transforms (XSLT), XML Annotations, .NET XML
classes such as XmlReader, XPathNavigator
• Simpler classes: XDocument, XElement,
XAttribute, XCData, XComment
Linq To XML Overview - 4
How to use:
• Be sure that the Visual Studio project targets
the 3.5 (or above) framework.
• Add a reference to System.Linq.Dll
• Add using or imports statements for
System.Linq and System.Xml.Linq
• Be sure to catch System.Xml.XmlException or
other exceptions for malformed and invalid
XML
Linq To XML Overview - 5
Demos:
1. Loading XML from a file (C# and VB)
2. Loading XML from a string literal
3. Creating an XML tree in C#
4. Creating an XML Document with multiple
namespaces
5. Querying an XML document with namepaces
6. XML Document Validation
7. XSL Document Transformation
Linq To XML Overview - 6
XML Goodies in Visual Studio, SDK
• XSD.EXE can generate .NET classes from XSD
• SVCUTIL.EXE can generate WCF DataContracts and
services from XSD
• Visual Studio allows XSLT compilation and debugging.
• Linq To XSD:
(http://www.hanselman.com/blog/LINQToEverything
LINQToXSDAddsMoreLINQiness.aspx,http://linqtoxsd.
codeplex.com/,
http://www.netframeworkdev.com/net-base-class-
library/linq-to-xsd-how-to-use-linq-to-xsd-
functionality-in-general-application-2782.shtml,
http://aspalliance.com/chapters/chapters/184719254
8/linq%20-%20sample%20chapter.pdf)
Linq To XML Overview - Final Links
Links on XML, LINQ to XML
• O’Reilly XML Site: http://www.xml.com/
• ZVON.org: http://www.zvon.org/index.php
• W3Schools.com: http://www.w3schools.com/xml/default.asp
• XML Pitstop: http://www.xmlpitstop.com/
• LINQ on MSDN: http://msdn.microsoft.com/en-
us/library/bb397926.aspx
• LINQ to XML on MSDN: http://msdn.microsoft.com/en-
us/library/bb387098.aspx
• Get Started with LINQ to XML: http://www.asp.net/learn/linq-
videos/video-215.aspx
• Enable XML IntelliSense and Use XML Namespaces
http://www.asp.net/learn/linq-videos/video-216.aspx
• Create Excel Spreadsheets using LINQ to XML
http://www.asp.net/learn/linq-videos/video-218.aspx

Linq To XML Overview

  • 1.
    Linq To XMLOverview Dale Hawthorne For Sherwin Williams
  • 2.
    Linq To XMLOverview -2 Previous .NET Options: • XmlDocument: Implements DOM, processor and memory intensive due to recursion, loading entire document in a tree before proceeding • XmlReader, XmlWriter, XslCompiledTransform, XmlNamespaceManager: Fast but code implementations are difficult to understand, not very readable
  • 3.
    Linq To XMLOverview -3 Advantages of Linq To XML: • Built over XMLWriters and XMLReaders, will generally be faster than XmlDocument class • Uses Linq query syntax, so processing XML more like asking or answering a question than parsing a document • Works with existing XML Schemas (.XSD), XML Transforms (XSLT), XML Annotations, .NET XML classes such as XmlReader, XPathNavigator • Simpler classes: XDocument, XElement, XAttribute, XCData, XComment
  • 4.
    Linq To XMLOverview - 4 How to use: • Be sure that the Visual Studio project targets the 3.5 (or above) framework. • Add a reference to System.Linq.Dll • Add using or imports statements for System.Linq and System.Xml.Linq • Be sure to catch System.Xml.XmlException or other exceptions for malformed and invalid XML
  • 5.
    Linq To XMLOverview - 5 Demos: 1. Loading XML from a file (C# and VB) 2. Loading XML from a string literal 3. Creating an XML tree in C# 4. Creating an XML Document with multiple namespaces 5. Querying an XML document with namepaces 6. XML Document Validation 7. XSL Document Transformation
  • 6.
    Linq To XMLOverview - 6 XML Goodies in Visual Studio, SDK • XSD.EXE can generate .NET classes from XSD • SVCUTIL.EXE can generate WCF DataContracts and services from XSD • Visual Studio allows XSLT compilation and debugging. • Linq To XSD: (http://www.hanselman.com/blog/LINQToEverything LINQToXSDAddsMoreLINQiness.aspx,http://linqtoxsd. codeplex.com/, http://www.netframeworkdev.com/net-base-class- library/linq-to-xsd-how-to-use-linq-to-xsd- functionality-in-general-application-2782.shtml, http://aspalliance.com/chapters/chapters/184719254 8/linq%20-%20sample%20chapter.pdf)
  • 7.
    Linq To XMLOverview - Final Links Links on XML, LINQ to XML • O’Reilly XML Site: http://www.xml.com/ • ZVON.org: http://www.zvon.org/index.php • W3Schools.com: http://www.w3schools.com/xml/default.asp • XML Pitstop: http://www.xmlpitstop.com/ • LINQ on MSDN: http://msdn.microsoft.com/en- us/library/bb397926.aspx • LINQ to XML on MSDN: http://msdn.microsoft.com/en- us/library/bb387098.aspx • Get Started with LINQ to XML: http://www.asp.net/learn/linq- videos/video-215.aspx • Enable XML IntelliSense and Use XML Namespaces http://www.asp.net/learn/linq-videos/video-216.aspx • Create Excel Spreadsheets using LINQ to XML http://www.asp.net/learn/linq-videos/video-218.aspx