c# qr codes LINQ to XML transformations in Visual C#.NET

Draw QR Code 2d barcode in Visual C#.NET LINQ to XML transformations

10.4.1 LINQ to XML transformations
QR Code ISO/IEC18004 Creator In C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Scan QR Code JIS X 0510 In Visual C#.NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
To see the powerful transformational capabilities offered by LINQ to XML, let s transform the XML from listing 10.22 into an XHTML document that can be shown in a browser.
Paint ANSI/AIM Code 39 In Visual C#
Using Barcode generator for VS .NET Control to generate, create Code 3/9 image in VS .NET applications.
www.OnBarcode.com
Encoding Data Matrix In C#
Using Barcode drawer for .NET framework Control to generate, create DataMatrix image in VS .NET applications.
www.OnBarcode.com
Listing 10.22 The XML to be transformed
Generating UPC A In Visual C#
Using Barcode encoder for .NET framework Control to generate, create UPCA image in VS .NET applications.
www.OnBarcode.com
Generate Barcode In C#
Using Barcode generation for VS .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
< xml version="1.0" encoding="utf-8" > <books> <book> <title>Linq in Action</title> <author>Fabrice Marguerie</author> <author>Steve Eichert</author> <author>Jim Wooley</author> <publisher>Manning</publisher> </book> <book> <title>Ajax in Action</title> <author>Dave Crane</author> <publisher>Manning</publisher> </book> <book> <title>Enterprise Application Architecture</title> <author>Martin Fowler</author> <publisher>APress</publisher> </book> </books>
EAN13 Generator In Visual C#.NET
Using Barcode generator for Visual Studio .NET Control to generate, create European Article Number 13 image in .NET applications.
www.OnBarcode.com
Printing OneCode In C#
Using Barcode generation for Visual Studio .NET Control to generate, create 4-State Customer Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
When we re finished with our transformation, we ll end up with the following XHTML:
Drawing QR Code ISO/IEC18004 In Objective-C
Using Barcode creation for iPad Control to generate, create Denso QR Bar Code image in iPad applications.
www.OnBarcode.com
QR Code 2d Barcode Scanner In C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
<html> <body> <h1>LINQ Books Library</h1>
Generating Barcode In None
Using Barcode creator for Office Excel Control to generate, create Barcode image in Microsoft Excel applications.
www.OnBarcode.com
Recognize EAN13 In C#.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Transforming XML
Barcode Maker In None
Using Barcode generator for Word Control to generate, create Barcode image in Microsoft Word applications.
www.OnBarcode.com
Encode Barcode In Java
Using Barcode drawer for BIRT Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
<div> <b>LINQ in Action</b> By: Fabrice Marguerie, Steve Eichert, Jim Wooley Published By: Manning </div> <div> <b>AJAX in Action</b> By: Dave Crane Published By: Manning </div> <div> <b>Patterns of Enterprise Application Architecture</b> By: Martin Fowler Published By: APress </div> </body> </html>
EAN / UCC - 13 Reader In VS .NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Creation In Java
Using Barcode creator for Eclipse BIRT Control to generate, create Data Matrix image in Eclipse BIRT applications.
www.OnBarcode.com
In order to transform the XML shown in listing 10.22 into this XHTML, we ll need to take advantage of the axis methods provided by LINQ to XML as well its functional construction capabilities. Let s start by examining how we can create the resulting XHTML using functional construction and for the moment ignore the XML that we re going to transform. To get a jump start, we can copy the resulting XHTML to the clipboard and use the Paste XML as LINQ Visual Studio .NET add-in to create our functional construction code for building the desired XML. When we do so, we end up with listing 10.23.
Paint UPCA In Objective-C
Using Barcode maker for iPad Control to generate, create UPC A image in iPad applications.
www.OnBarcode.com
Creating UPC-A In VS .NET
Using Barcode creation for ASP.NET Control to generate, create GTIN - 12 image in ASP.NET applications.
www.OnBarcode.com
NOTE
GS1 128 Drawer In Objective-C
Using Barcode drawer for iPad Control to generate, create EAN / UCC - 13 image in iPad applications.
www.OnBarcode.com
EAN / UCC - 13 Maker In None
Using Barcode drawer for Online Control to generate, create EAN13 image in Online applications.
www.OnBarcode.com
The Paste XML as LINQ Visual Studio add-in is shipped as a sample. To use it in Visual Studio 2008, you have to compile and install the add-in from the source code provided. The add-in can be found in the LinqSamples directory of the \Program Files\Visual Studio 9.0\Samples\CSharpSamples.zip file.
Listing 10.23 The LINQ to XML code created via the Paste XML as LINQ Visual Studio .NET add in
XElement xml = new XElement("html", new XElement("body", new XElement("h1", "LINQ Books Library"), new XElement("div", new XElement("b", "LINQ in Action"), " By: Fabrice Marguerie, Steve Eichert, Jim Wooley\n" + " Published By: Manning\n" ), new XElement("div", new XElement("b", "AJAX in Action"),
Query and transform XML with LINQ to XML
" " By: Dave Crane\n" + Published By: Manning\n"
), new XElement("div", new XElement("b", "Patterns of Enterprise Application Architecture"), " By: Martin Fowler\n" + " Published By: APress\n" ) ) );
As we can see, the Paste XML as LINQ add-in converted the XHTML into a single functional construction statement that creates each item contained within the XML. While the XML isn t perfect, it provides a good starting point. When transforming XML, you may often find it beneficial to start with the end in mind and work your way backward. Now that we have a template for the XHTML we want to produce, we can incorporate LINQ to XML queries and take advantage of the rich support for embedding query expressions within functional construction statements. Before incorporating our query expressions with the functional construction statements, lets write a query to retrieve the data out of our XML. See listing 10.24.
Listing 10.24 Retrieve the title, publisher, and authors for each book within the XML
var books = from book in booksXml.Descendants("book") select new { Title = (string)book.Element("title"), Publisher = (string)book.Element("publisher"), Authors = String.Join(", ", book.Descendants("author") .Select(a => (string)a).ToArray()) };
With this query, we ve selected the title, publisher, and authors out of the XML file. We ve done extra work to format the list of author names as a commaseparated list to match our desired output format. Now that we have our functional construction statements and our query, it s time to combine the two into a single LINQ to XML transformation. See listing 10.25.
Transforming XML
Listing 10.25 Transform XML into XHTML with LINQ to XML transformations
XElement html = new XElement("html", new XElement("body", new XElement("h1", "LINQ Books Library"), from book in booksXml.Descendants("book") select new XElement("div", new XElement("b", (string)book.Element("title")), "By: " + String.Join(", ", book.Descendants("author") .Select(b => (string)b).ToArray()) + "Published By: " + (string)book.Element("publisher") ) ) );
The resulting XHTML can be seen in figure 10.2. As we ve illustrated, LINQ to XML provides powerful transformation capabilities. Rather than having to learn a new language, such as XSLT, developers can leverage the knowledge they ve already gained for creating XML using functional construction and their knowledge for querying XML using LINQ queries. By providing a single construction method for creating XML from scratch as well as creating XML from other XML via transformations, LINQ to XML provides a consistent programming model. While LINQ to XML offers powerful transformations, the LINQ to XML team also recognized that a lot of existing applications have large investments in XSLT as a transformation technology. As such, they ve provided support for transforming LINQ to XML objects using XSLT. Let s explore how we can use XSLT to produce the same output that we ve produced in figure 10.2 with LINQ to XML transformations.
Copyright © OnBarcode.com . All rights reserved.