Query and transform XML with LINQ to XML in Visual C#

Drawing QR-Code in Visual C# Query and transform XML with LINQ to XML

Query and transform XML with LINQ to XML
Denso QR Bar Code Generation In Visual C#.NET
Using Barcode creation for .NET framework Control to generate, create QR Code ISO/IEC18004 image in VS .NET applications.
www.OnBarcode.com
Scanning QR-Code In C#.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Before moving on to our next LINQ to XML axis method, let s revisit the XML fragment that we re working with and talk about what s next. We started our journey by selecting the first category under the root element in listing 10.1. The following XML fragment is the result:
Barcode Drawer In C#.NET
Using Barcode drawer for .NET framework Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Generate Quick Response Code In C#
Using Barcode printer for .NET framework Control to generate, create QR Code JIS X 0510 image in VS .NET applications.
www.OnBarcode.com
<category name=".NET"> <books> <book>CLR via C#</book> <book>Essential .NET</book> </books> </category>
Barcode Creation In C#
Using Barcode drawer for Visual Studio .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
USS Code 39 Printer In Visual C#
Using Barcode creation for .NET framework Control to generate, create Code39 image in .NET framework applications.
www.OnBarcode.com
Once we have the XElement in hand, we output the name of the category to the console with the help of the Attribute query axis method. Next we need to query the category XElement for all the book elements contained within it. Unfortunately, we need to select multiple elements, so we can t use the Element method that we ve already learned about. It looks like it s time to learn about the Elements axis method.
EAN 13 Encoder In Visual C#.NET
Using Barcode encoder for VS .NET Control to generate, create GTIN - 13 image in .NET applications.
www.OnBarcode.com
ANSI/AIM Codabar Creator In C#.NET
Using Barcode generator for .NET Control to generate, create ABC Codabar image in Visual Studio .NET applications.
www.OnBarcode.com
10.1.3 Elements
Quick Response Code Generator In None
Using Barcode drawer for Font Control to generate, create Quick Response Code image in Font applications.
www.OnBarcode.com
Quick Response Code Drawer In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
www.OnBarcode.com
The Elements axis method is similar to the Element query axis method; the primary difference is that rather than returning the first matching XElement, Elements returns all matches. Given this, it shouldn t be surprising that Elements returns an IEnumerable of XElement objects, rather than a single XElement. Like Element, Elements accepts an XName as a parameter. In our case, we re looking for all <book> elements so we ll provide book as our parameter to Elements. Since the <book> elements aren t directly under the category XElement that we selected in sections 10.1.1 and 10.1.2, we ll need to select the <books> element with the Element query axis method, and then call Elements as shown in listing 10.4.
Creating GTIN - 12 In None
Using Barcode printer for Software Control to generate, create UPC-A Supplement 5 image in Software applications.
www.OnBarcode.com
Draw Barcode In Objective-C
Using Barcode maker for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Listing 10.4 Select all the child book elements using the Elements query axis method
PDF-417 2d Barcode Generator In .NET Framework
Using Barcode creator for .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
Matrix Maker In .NET
Using Barcode printer for ASP.NET Control to generate, create 2D image in ASP.NET applications.
www.OnBarcode.com
XElement root = XElement.Load("categorizedBooks.xml"); XElement dotNetCategory = root.Element("category"); XAttribute name = dotNetCategory.Attribute("name"); XElement books = dotNetCategory.Element("books"); IEnumerable<XElement> bookElements = books.Elements("book"); Console.WriteLine((string) dotNetcategory); foreach(XElement bookElement in bookElements) { Console.WriteLine(" - " + (string)bookElement); }
EAN 13 Encoder In .NET
Using Barcode printer for VS .NET Control to generate, create UPC - 13 image in .NET framework applications.
www.OnBarcode.com
Print PDF 417 In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create PDF-417 2d barcode image in ASP.NET applications.
www.OnBarcode.com
LINQ to XML axis methods
Making ANSI/AIM Code 39 In VS .NET
Using Barcode creator for .NET Control to generate, create ANSI/AIM Code 39 image in VS .NET applications.
www.OnBarcode.com
QR Code Drawer In Objective-C
Using Barcode encoder for iPad Control to generate, create QR-Code image in iPad applications.
www.OnBarcode.com
When we run the code, we get the following results:
Decode PDF-417 2d Barcode In C#.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Read UPC-A Supplement 5 In Visual C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
.NET - CLR via C# - Essential .NET
In addition to allowing us to find all elements with a given name, the Elements method also has a parameterless overload that can be used to retrieve all the children of an XElement. In the listing, we could have called the parameterless version of Elements since the <books> element only contains <book> elements as children. By leveraging the Element, Attribute, and Elements axis methods, we ve successfully read a set of details out of our sample XML and accomplished our goal. We didn t set our sights that high, but nevertheless we ve learned about three essential LINQ to XML axis methods that we ll use when constructing more complex LINQ to XML queries. It s important to remember that Elements only searches the elements that are direct children of the XElement that it s called on. Sometimes rather than needing just the children of the current element, we want to look at all the elements that exist at any level beneath the current element. It s for these scenarios that the LINQ to XML API provides the Descendants axis method.
10.1.4 Descendants
The Descendants axis method works in the same way as the Elements method, but instead of limiting the elements returned to those that are direct children of the current element, Descendants will traverse all the elements underneath the current element. The Descendants axis method is helpful when you want to retrieve all the elements with a particular XName, but you re not sure where in the tree they live. The Descendants axis method has two overloads. The first overload accepts an XName and returns all elements anywhere underneath the current element with the provided XName. To retrieve every descendant, regardless of XName, you can call Descendants without any parameters. We re once again going to use the XML we introduced in listing 10.1. This time, instead of looking for all the books within a single category, we d like to return every book, no matter what category it s in. Since the book elements exist at different levels within the XML, we can t use the Elements axis method. Instead, we ll use the Descendants axis method. To retrieve every book within our XML, we can write the code shown in listing 10.5.
Copyright © OnBarcode.com . All rights reserved.