ComponentOne LiveLinq > How to Use LiveLinq > Query Collection > Using XML Data |
LiveLinq can query data directly from XML in memory (stored in a LINQ to XML XDocument class). It dramatically speeds up LINQ to XML performance by supporting XML indexing (not supported by the regular LINQ to XML).
To use LiveLinq to XML, you need to add
using C1.LiveLinq.LiveViews.Xml;
to your source file.
Then you need to create some live views over your XML data. Unlike LiveLinq to Objects and LiveLinq to DataSet, where you can query data without live views, in LiveLinq to XML queries and live views are always used together. You are using LiveLinq to XML versions of LINQ query operators if you apply them to a live view. Otherwise, they will be the standard, not LiveLinq query operators. To start creating live views, simply apply the extension method AsLive() to an XDocument:
XDocument doc = …
View<XDocument> docView = doc.AsLive();
Once you have a live view, you can use the familiar (from LINQ to XML) query operators Elements, Descendants and others (see XmlExtensions Class) as well as all Standard Query Operators Supported in Live Views to define a live view. For example,
View<XElement> orders = docView.Descendants("Orders");
defines the collection of orders in the document. This collection is live; it is automatically kept up to date with the data in the XDocument that can be changed by your program. As a result, you can work with data in this collection in the same way as you would work with any dynamic collection, including ADO.NET DataTable or DataView. In particular, you can create indexes over it and use them for speeding up queries and for fast programmatic searches as shown in other sections of this documentation. It means that LiveLinq to XML makes it possible to work with XML data in memory without losing performance, so it is no longer necessary to create custom collection classes or use ADO.NET or other frameworks working with XML data. All you need is LINQ to XML with addition of LiveLinq.
Once you have some live views defined over XML data, you can query them. For example,
var query = from Order in orders.AsIndexed()
where (string)Order.IndexedAttribute("CustomerID") == "ALFKI"
gives you a query for orders of a particular customer.
Note: It is important to distinguish between live views and queries in LiveLinq to XML. Since you always start with a live view, your query will be a live view by default, unless you specify otherwise. In the example above, we used AsIndexed() to specify that we only need a query, don’t need that query to define a live view. Live views extend query functionality, so they can be used instead of queries. However, live views have performance overhead, so you should avoid using a live view where a simple query would suffice. As a general rule, avoid creating live views and throwing them away after using them just once to get results. Live views are designed to remain active (hence they are called live) and show up-to-date data. |