LINQ to XML
Back to TOCJust VideosCondensed TOC
Access XML documents using Language Integrated Query (LINQ)
- Performance of Chained Queries
- Atomized XName and XNamespace Objects
- Preatomization of XName Objects
- Statically Compiled Queries
- Performance of LINQ to XML
- Writing Robust LINQ to XML Code that Performs Well
- Removing Comments and Personal Information, and Accepting Revisions in an Open XML Document Stored in SharePoint
- Generating a LINQ to XML Tree from Anonymous Types
- Using LINQ to XML Events and Annotations to Track if an XML Tree has Changed
- Comparing LINQ to XML and XPath
- A More Robust Approach for Handling XName Objects in LINQ to XML
- Writing Succinct Code to move XElement and XAttribute Objects from One LINQ to XML Tree to Another
- Getting Started with SharePoint Web Services using LINQ to XML
- Equality Semantics of LINQ to XML Trees
- Manually Cloning LINQ to XML Trees
- The Composability Thought Process for LINQ
- Ease of Maintenance of LINQ Code
- Computing Deep Hash Codes using LINQ to XML
- Ease of Maintenance of LINQ Code (2)
- Querying LINQ to XML Nodes in Reverse Document Order with Better Performance
- Document-Centric Transforms using LINQ to XML
- Recursive Approach to Pure Functional Transformations of XML
- Align Attributes when Formatting XML using LINQ to XML
- Writing Entity References using LINQ to XML
- Convert XDocument to XmlDocument (and Convert XmlDocument to XDocument)
- Serializing Encoded XML Documents using LINQ to XML
- Controlling Namespace Serialization of LINQ to XML
- Simulating Virtual Extension Methods
- Refinement: Generating C# code from an XML Tree using Virtual Extension Methods
- Working with Optional Elements and Attributes in LINQ to XML Queries
- Review of XPath Semantics of LINQ to XML
- Custom Formatting of XML using LINQ to XML
- Screen-Cast: Functional Programming Utility Methods and Classes for Open XML
One of the most important aspects of LINQ (and LINQ to XML) is that performance of chained queries can be effectively the same as if you write one larger, more complicated query. (Article)
XName and XNamespace objects are atomized. This yields performance benefits for queries: comparing two atomized names for equality means that the underlying intermediate language (IL) only needs to determine if the two references point to the same object. The underlying system does not need to do any string comparisons, which would be time consuming. (Article)
One approach that you can take to gain some performance is to pre-atomize XName objects. This would yield performance benefits when creating a large XML tree where specific names are repeated. The approach is to declare and initialize XName objects before the construction of the XML tree, and then to use the XName objects rather than specifying strings for the element and attribute names. (Article)
One of the most important performance benefits that you will note when comparing LINQ to XML and XmlDocument is that queries in LINQ to XML are statically compiled, whereas XPath queries must be interpreted at runtime. (Article)
There are a number of issues and dynamics that we can talk about with regards to performance of LINQ to XML. (Article)
Lists some important posts about LINQ to XML that explains approaches that you can take to write LINQ to XML code that has fewer bugs. (Article)
This post presents a custom application page in SharePoint that uses Open XML, the Open XML SDK and LINQ to XML to accept revisions, remove comments, and remove personal information from an Open XML word processing document. (Article)
Shows how to write code to generate an XML tree from a hierarchy of anonymous types. In addition, it compares and contrasts writing code in the imperative style to writing code in the declarative style. (Article)
You may optionally be making a number of modifications to a very large XDocument object. Because of a complicated algorithm, you may not necessarily know ahead of time whether you will be making changes. If you don’t make any changes, then you don’t want to unnecessarily serialize the tree and save it to disk, or send it over the wire. This post presents a simple technique to determine if an XDocument has ever been modified since deserialization or creation. (Article)
Provides an overview of the similarities and differences between XPath and LINQ to XML (Article)
Pre-atomizing XName objects enables you to write more reliable code that performs well. (Article)
This post presents an interesting idiom of LINQ to XML that makes it easy to write short, succinct code when creating one XML tree from another. (Article)
Web services are one of the most effective and convenient ways for casual developers to access SharePoint lists and document libraries. They have a reputation for being a bit difficult; LINQ to XML can make them easier to use. This post presents the basic steps for getting started with Windows SharePoint Services Web Services using LINQ to XML (either C# or VB). (Article)
In certain scenarios, it is important to be able to compare two XML trees for equivalence. For example, if you are writing a web service that serves results of queries, and you want to cache query results so that duplicate queries use previously cached results instead of always accessing the underlying database. However, the senders of those queries may potentially be using a variety of tools to generate the queries, and these tools may introduce trivial differences into the XML. (Article)
There are a variety of circumstances where you want to clone a LINQ to XML tree while making modifications to the cloned tree. It’s possible to write a very small amount of recursive code to do this. (Article)
This post demonstrates the thought process of developing a somewhat more involved query. (Article)
I believe that it is easier to maintain code that is written in the functional style. For one thing, this is the very reason for many of the characteristics of functional code. No state is maintained, so we don’t have to worry about corrupting any state. If a variable is in scope, then the variable has its value, and it will never have another value. And the idea of composability is all about being able to inject/surround/refactor code without making it brittle. In this post, I’m going to show the process of maintaining and modifying a somewhat more involved query. (Article)
In some scenarios, it is useful to compute a ‘deep’ hash code using LINQ to XML. If you are writing a server-side application that caches queries expressed in XML, then you may want to store these queries in a hash table. (Article)
In two previous posts, I developed a somewhat involved query to search through a word processing document for style names and/or paragraph content. This is a query that I’m developing for the PowerTools for Open XML project. In those posts, as I evolved the query, I showed each iteration of it, highlighting the changes I made. This post continues modifying that query. (Article)
Occasionally I need to query LINQ to XML nodes in reverse document order. However, nodes in LINQ to XML are forward-linked only, so getting the previous sibling element can perform slowly. This post presents an approach for significant performance gains. (Article)
When thought of in a certain way, XML documents come in two flavors – data-centric and document-centric. Further, there are two types of document-centric documents. This post presents my thoughts about approaches to various types of document-centric transformations – data-centric to document-centric, document-centric to data-centric, and document-centric to document-centric. (Article)
Writing pure functional transformations a in a recursive style enables us to put together interesting transformations in a very small amount of code. Using some specific techniques that allow us to write this code very concisely, this approach takes advantage of some perhaps obscure semantics of LINQ to XML. (Article)
Sometimes an XML element contains a large number of attributes, and the values of the attributes may be long. When written to the console, such lines wrap, making it hard to read the XML. This post shows how you can use appropriate settings for an XmlWriter so that the attributes are aligned, and each attribute is on a separate line. (Article)
As delivered, it is difficult to write entities using LINQ to XML. This post presents a small hack that enables you to serialize XML entities. (Article)
Some time ago, I blogged about an approach for converting an XElement object to an XmlNode object, and vice versa. This is useful when you want to use a programming interface that takes and returns objects of type XmlNode, but you want to use the expressiveness and power of LINQ to XML for your code that modifies the XML tree. There are a few occasions where this isn’t good enough – you need to convert from an XDocument to XmlDocument and back. (Article)
Writing encoded (utf-8, utf-16, etc.) documents using LINQ to XML is pretty straight-forward, but there is one interesting dynamic of the semantics. When serializing to a file on disk, then you can set the encoding in the XML declaration, and the resulting XML document will be serialized as you wish. However, if you are writing to a stream that supports only one specific encoding, then the XML document will automatically be encoded to match the stream, overriding your specified desired encoding, and the XML declaration will be adjusted accordingly. (Article)
You can explicitly control how LINQ to XML serializes the namespaces for an XML tree. You can control whether a namespace is serialized as the default namespace for a tree, or whether the namespace will use a prefix. (Article)
Similating virtual extension methods can help in solving problems when dealing with a class hierarchy that uses polymorphism. (Article)
Discusses code that can generate C# code to generate any arbitrary XML tree. (Article)
Keywords: LINQ to XML
Often XML schemas allow for optional elements and attributes. When you write queries on these elements or attributes, you may be tempted to write code that does lots of testing for null. There is a better way to do this, laid out in this post. (Article)
In this post, I review the XPath semantics of LINQ to XML, and show some concrete examples of how I use those semantics in the XPath-in-Content-Controls approach to Open XML WordprocessingML document generation. (Article)
Demonstrates the ability to customize the serialization of XML trees when using LINQ to XML. (Article)
In the process of developing the PowerTools for Open XML, over the years, I have developed some needed core functionality and utility methods, particularly for writing LINQ to XML code that queries and transforms Open XML documents. (Video)