Common LINQ to XML scenarios in C#.NET

Make Denso QR Bar Code in C#.NET Common LINQ to XML scenarios

Common LINQ to XML scenarios
QR Code ISO/IEC18004 Printer In Visual C#.NET
Using Barcode generator for VS .NET Control to generate, create Denso QR Bar Code image in .NET framework applications.
www.OnBarcode.com
QR Code JIS X 0510 Scanner In Visual C#.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
</review> </reviews> </book> <book> <title>Patterns of Enterprise Application Architecture</title> </book> </books>
Create Code 3/9 In Visual C#.NET
Using Barcode generation for VS .NET Control to generate, create Code 39 Full ASCII image in .NET framework applications.
www.OnBarcode.com
USS-128 Maker In Visual C#
Using Barcode generator for VS .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications.
www.OnBarcode.com
As we can see, the XML contains a set of information about books. If we move our attention to the classes shown in figure 11.1, we can see that we have a series of objects that correlate to the data contained in the XML file in listing 11.1.
European Article Number 13 Creator In C#
Using Barcode drawer for Visual Studio .NET Control to generate, create European Article Number 13 image in VS .NET applications.
www.OnBarcode.com
Creating UPC Symbol In Visual C#
Using Barcode creation for .NET Control to generate, create UPCA image in .NET applications.
www.OnBarcode.com
Figure 11.1 Class diagram
Create Quick Response Code In C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create QR Code image in .NET framework applications.
www.OnBarcode.com
ISSN Maker In Visual C#.NET
Using Barcode generation for .NET Control to generate, create ISSN - 10 image in .NET applications.
www.OnBarcode.com
Building objects from XML
Making QR Code JIS X 0510 In Objective-C
Using Barcode generator for iPhone Control to generate, create Quick Response Code image in iPhone applications.
www.OnBarcode.com
QR Code Creator In Java
Using Barcode creation for Java Control to generate, create QR Code JIS X 0510 image in Java applications.
www.OnBarcode.com
Our goal is to take the data contained in the XML file and create a list of Book objects. In addition to populating the Book object, we ll also populate the Subject, Publisher, Authors, and Reviews properties of the Book with the corresponding information in the XML file. With our goal of creating book objects from the XML out of the way, let s a look at the technique we ll use to accomplish our goal.
PDF 417 Recognizer In C#
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Barcode Drawer In Java
Using Barcode creation for BIRT Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
11.1.2 Implementation
Barcode Creator In Java
Using Barcode printer for Eclipse BIRT Control to generate, create Barcode image in BIRT applications.
www.OnBarcode.com
Creating PDF417 In Java
Using Barcode printer for Java Control to generate, create PDF-417 2d barcode image in Java applications.
www.OnBarcode.com
As we saw in the previous chapter, LINQ to XML provides powerful transformation capabilities. In addition to being able to transform XML to alternate XML formats, LINQ to XML also allows us to transform XML into other data structures, such as in-memory objects. To build our objects from XML, we ll write several queries that leverage the axis methods provided by LINQ to XML. The queries will retrieve the baseline book information out of the XML document, as well as the publishers, authors, and reviews. We ll build our objects using the new object initializer syntax available in C# 3.0. We ll also explore how we can nest query expressions to read data that is in repeating elements, such as the authors and reviews. Before diving into the details of how we can construct objects from XML, let s figure out how to load our XML document into an XElement. To load the XML document, we can use the static Load method on the XElement class.
Quick Response Code Generator In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications.
www.OnBarcode.com
Draw Barcode In None
Using Barcode encoder for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
XElement booksXml = XElement.Load("books.xml");
Making QR Code ISO/IEC18004 In Java
Using Barcode generator for Android Control to generate, create QR-Code image in Android applications.
www.OnBarcode.com
EAN / UCC - 13 Generator In .NET Framework
Using Barcode encoder for .NET Control to generate, create European Article Number 13 image in Visual Studio .NET applications.
www.OnBarcode.com
Once the XML is loaded, we need to figure out how we can get the data within the XML into our Book objects. Since the book details are contained within the <book> element, our first step is to retrieve all the <book> elements within the XML document. As we learned in the previous chapter, the Elements query axis method can be used to select all elements with a particular name, so we ll select every book element with the following code:
Code 3/9 Drawer In VS .NET
Using Barcode drawer for ASP.NET Control to generate, create Code 39 Extended image in ASP.NET applications.
www.OnBarcode.com
UPC Symbol Scanner In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
IEnumerable<XElement> bookElements = booksXml.Elements("book");
Our call to the Elements query axis method will return an IEnumerable<XElement> that represents the books within our XML document. In order to build our Book object from the XElement objects, we ll need to read data out of the relevant child elements. The easiest way to construct our book object with the data in our XML is to alter our code to use the object initializer syntax available in C# 3.0. Rather than simply selecting the XElement objects as we did earlier, we ll update our query to create new book instances by adding a select clause to our query.
Common LINQ to XML scenarios
We ll assign the values contained in the child elements of the <book> element to the corresponding property on the Book object, as shown in listing 11.2.
Listing 11.2 Create Book objects from the XML using object initializers
var books = from bookElement in booksXml.Elements("book") select new Book { Title = (string)bookElement.Element("title"), PublicationDate = (DateTime)bookElement.Element("publicationDate"), Price = (decimal)bookElement.Element("price"), Isbn = (string)bookElement.Element("isbn"), Notes = (string)bookElement.Element("notes"), Summary = (string)bookElement.Element("summary") };
To read the details of the book contained in the child elements, we select the relevant element with the Elementaxis method and use the explicit cast operators defined on XElement to convert the value to the proper data types. While this query gives us the basic details for our books, it doesn t include the data that is nested within child nodes, such as the list of authors and reviews. To include the authors, we can update our query to include a nested query that returns a list of Author objects that matches the author s details contained in the XML. Since the <author> elements are not directly below the <book> element, we can use the Descendants query axis method and once again use the object initializer syntax to build our author object with the data contained within the XML.
... Authors = from authorElement in bookElement.Descendants("author") select new Author { FirstName = (string)authorElement.Element("firstName"), LastName = (string)authorElement.Element("lastName") } ...
Copyright © OnBarcode.com . All rights reserved.