c# code 39 barcode generator WORKING WITH LINQ TO XML in Visual C#

Maker Code 39 Full ASCII in Visual C# WORKING WITH LINQ TO XML

CHAPTER 13 WORKING WITH LINQ TO XML
Encoding ANSI/AIM Code 39 In Visual C#.NET
Using Barcode drawer for .NET Control to generate, create Code 3/9 image in .NET applications.
www.OnBarcode.com
Code 39 Full ASCII Reader In Visual C#
Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
LocalName property of the XName class returns the name of the underlying element without a namespace qualifier. This name is then used to add the root node of the TreeView. Since we are interested only in the elements, the code uses the Elements() method to retrieve all the child elements of the <employees> element. The employeeid attribute of the <employee> element is accessed via the Attribute() method, which accepts the name of the attribute whose value is to be retrieved and returns an XAttribute instance representing that attribute. The Value property of the XAttribute class gives us the value of the attribute. A TreeNode for that employee is then added to the TreeView. The HasElements Boolean property of the XElement class tells us whether an element has any child elements. If so, we iterate through all the descendents of the current <employee> element using its Descendents() method. In our example the Descendents() method will return four XElements for the <firstname>, <lastname>, <homephone>, and <notes> elements, respectively. The Value property of the XElement class returns the inner content of the element which is then displayed in a TreeNode.
GTIN - 12 Printer In Visual C#
Using Barcode encoder for Visual Studio .NET Control to generate, create GTIN - 12 image in .NET framework applications.
www.OnBarcode.com
Create USS-128 In C#.NET
Using Barcode drawer for .NET framework Control to generate, create GS1-128 image in VS .NET applications.
www.OnBarcode.com
Note In the preceding example, the structure of the XML document was known to us. In cases where the nesting of XML tags is not known at design time, you may use recursive code that traverses the entire nested hierarchy.
EAN 13 Printer In Visual C#.NET
Using Barcode generator for .NET framework Control to generate, create EAN-13 image in .NET applications.
www.OnBarcode.com
Code 3 Of 9 Creation In Visual C#
Using Barcode creator for .NET Control to generate, create Code 3/9 image in VS .NET applications.
www.OnBarcode.com
Looking for Specific Elements and Attributes
Linear Barcode Generator In Visual C#
Using Barcode printer for .NET Control to generate, create Linear 1D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Generating EAN-8 Supplement 2 Add-On In Visual C#
Using Barcode generator for VS .NET Control to generate, create EAN-8 Supplement 2 Add-On image in .NET applications.
www.OnBarcode.com
Searching XML data is a common requirement in many scenarios. LINQ to XML provides powerful ways to search your data. You can use all the features of LINQ to create your queries and look up the XML data. Most commonly we need to search Elements matching a specific tag name Elements containing a specific value Attributes matching a specific value In the sections that follow, we will develop two applications: one allows you to search elements on the basis of their tag names, and the other allows you to grab one particular employee with a specific employee ID.
Code 39 Creator In Java
Using Barcode creator for Java Control to generate, create Code 3/9 image in Java applications.
www.OnBarcode.com
Code 3 Of 9 Recognizer In VS .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Retrieving Specific Elements Using the Descendents() Method
Painting Barcode In .NET
Using Barcode creator for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Generate Code-39 In None
Using Barcode encoder for Online Control to generate, create Code 39 Extended image in Online applications.
www.OnBarcode.com
When you wish to retrieve a set of elements with a certain tag name, the Descendents() method can come in handy. In order to illustrate the use of the Descendents() method, we will develop an application like the one shown in Figure 13-6. The application consists of a TextBox for specifying the tag name to search for. Upon clicking the Search button, the results will be displayed in a ListBox. Selecting a particular tag name from the ListBox will show its value in another read-only TextBox. The CheckBox titled Find only the first occurrence determines whether to find all instances of the tag name or end the search after the first match is found.
Make 2D In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create Matrix Barcode image in ASP.NET applications.
www.OnBarcode.com
Making Barcode In .NET
Using Barcode creation for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
C HA P TER 13 WO RK ING W IT H LI NQ T O XM L
Create Code39 In Java
Using Barcode creation for Java Control to generate, create Code 39 Full ASCII image in Java applications.
www.OnBarcode.com
PDF417 Scanner In Visual Studio .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Figure 13-6. Application for searching elements by tag name The code that makes the application work is shown in Listing 13-9. Listing 13-9. Using the Descendents() Method to Search Specific Elements XElement root = null; XElement[] datasource = null; private void Form1_Load(object sender, EventArgs e) { root = XElement.Load(Application.StartupPath + @"\employees.xml"); } private void button1_Click(object sender, EventArgs e) { textBox2.Text = ""; var subset = from item in root.Descendants(textBox1.Text) select item; if (!checkBox1.Checked) { datasource = subset.ToArray(); } else { if (subset.Count() > 0) { datasource = new XElement[1]; datasource[0] = subset.First(); } } listBox1.DataSource = datasource; listBox1.DisplayMember = "Name"; }
Barcode Maker In Java
Using Barcode printer for BIRT Control to generate, create Barcode image in BIRT applications.
www.OnBarcode.com
Decoding UPC-A In C#.NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 13 WORKING WITH LINQ TO XML
Code-128 Creator In Java
Using Barcode encoder for Android Control to generate, create Code 128 Code Set A image in Android applications.
www.OnBarcode.com
Make Barcode In None
Using Barcode creator for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { textBox2.Text= datasource[listBox1.SelectedIndex].Value; } The code declares two class-level variables: root and datasource. The root variable is of type XElement and holds a pointer to the root node of Employees.xml. The datasource variable is an array of type XElement and is used for data binding. The Employees.xml file is loaded into the root variable in the Load event of the form. The main job of searching a tag name happens in the Click event handler of the Search button. Notice the LINQ query shown in bold. It uses the Descendents() method of the XElement class. The Descendents() method has two overloads: one that doesn t take any parameters and one that takes an element name as the parameter. The former overload returns all the descendent elements of the current node, whereas the latter overload returns all the descendent elements that match the specified tag name. If the CheckBox is unchecked (i.e., we want to display all occurrences of the specified tag), then we convert the result into an array of XElement objects using the ToArray() method. Otherwise we create an array of XElements manually and store the first occurrence of the element in it. Notice the use of the First() method to retrieve just the first instance of the element. The datasource variable is then bound with the ListBox. In order to display the actual value of the element, the code handles the SelectedIndexChanged event of the ListBox. Inside it simply retrieves the selected XElement from the datasource array and displays its Value property in the TextBox. To test the application, run it from Visual Studio. Type firstname in the search TextBox, and click the Search button. The application should list three occurrences of the <firstname> tag in the ListBox. Click any of the instances, and you should see its value displayed in the other TextBox.
Copyright © OnBarcode.com . All rights reserved.