c# code 39 barcode generator Searching on the Basis of Attribute Values in C#.NET

Creation Code 39 in C#.NET Searching on the Basis of Attribute Values

Searching on the Basis of Attribute Values
Making Code 39 In Visual C#.NET
Using Barcode generation for .NET framework Control to generate, create Code 3 of 9 image in Visual Studio .NET applications.
www.OnBarcode.com
Code 39 Extended Recognizer In Visual C#
Using Barcode scanner for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
XML data often needs to be searched on the basis of attribute values and not just tag names. The Attributes() and Attribute() methods help you to do just that. To demonstrate the use of these methods, we will develop an application like the one shown in Figure 13-7.
Painting 2D Barcode In C#.NET
Using Barcode generation for VS .NET Control to generate, create Matrix 2D Barcode image in VS .NET applications.
www.OnBarcode.com
Make PDF-417 2d Barcode In Visual C#.NET
Using Barcode drawer for VS .NET Control to generate, create PDF 417 image in .NET framework applications.
www.OnBarcode.com
Figure 13-7. Application for searching attribute values
Draw QR Code In C#
Using Barcode maker for Visual Studio .NET Control to generate, create QR Code image in .NET framework applications.
www.OnBarcode.com
Print Barcode In C#.NET
Using Barcode generation for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
C HA P TER 13 WO RK ING W IT H LI NQ T O XM L
Drawing ECC200 In C#.NET
Using Barcode generation for .NET framework Control to generate, create Data Matrix image in VS .NET applications.
www.OnBarcode.com
International Standard Serial Number Creator In C#.NET
Using Barcode encoder for VS .NET Control to generate, create ISSN - 10 image in .NET applications.
www.OnBarcode.com
The application consists of a combo box that displays a list of employee IDs. Recollect that the Employees.xml file stores employee IDs in the employeeid attribute of the <employee> element. Upon selecting a specific employee ID and clicking the Show button, the details of that employee (first name, last name, home phone, and notes) are displayed. The code that does the searching is shown in Listing 13-10. Listing 13-10. Searching Attribute Values private void Form1_Load(object sender, EventArgs e) { root = XElement.Load(Application.StartupPath + @"\employees.xml"); var result = from item in root.Elements("employee") where item.Attributes("employeeid").Count() > 0 select item.Attribute("employeeid").Value; foreach (var obj in result) { comboBox1.Items.Add(obj); } } private void button1_Click(object sender, EventArgs e) { var result = from item in root.Elements("employee") where item.Attribute("employeeid"). Value == comboBox1.SelectedItem.ToString() select item; foreach (var obj in result) { label6.Text = obj.Element("firstname").Value; label7.Text = obj.Element("lastname").Value; label8.Text = obj.Element("homephone").Value; label9.Text = obj.Element("notes").Value; } } The code loads the Employees.xml file into an instance of XElement in the Load event of the form. It then calls the Elements() method on the root element by supplying the tag name as employee. This way, only the elements with the employee tag name are returned. The where clause then checks if these elements contain the employeeid attribute and, if so, returns the value of the employeeid attribute. The Attributes()method has two overloads: one takes no parameters and the other accepts the attribute name as the parameter. The former overload returns all the attributes (in the form of an XAttribute collection) of an element, whereas the latter one returns only the attributes with the specified name. The Attribute() method accepts the name of an attribute to retrieve and returns an XAttribute instance associated with it. Notice that the query selects just the attribute value. The foreach loop then iterates through the results and populates the employee IDs into the combo box.
Encode Code 3/9 In None
Using Barcode drawer for Excel Control to generate, create Code39 image in Excel applications.
www.OnBarcode.com
Generating Code-39 In Objective-C
Using Barcode drawer for iPhone Control to generate, create Code 39 Full ASCII image in iPhone applications.
www.OnBarcode.com
CHAPTER 13 WORKING WITH LINQ TO XML
Printing Data Matrix 2d Barcode In None
Using Barcode encoder for Font Control to generate, create ECC200 image in Font applications.
www.OnBarcode.com
Painting PDF 417 In Java
Using Barcode creator for Java Control to generate, create PDF-417 2d barcode image in Java applications.
www.OnBarcode.com
In the Click event handler of the Show button, the code fetches an element whose employeeid attribute matches the value selected in the combo box. The Value property of the XAttribute class represents the value of an attribute. The results are then displayed in the Label controls.
QR-Code Decoder In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
GS1 DataBar Creator In .NET
Using Barcode drawer for VS .NET Control to generate, create GS1 RSS image in .NET framework applications.
www.OnBarcode.com
Modifying XML Data
Print EAN128 In Objective-C
Using Barcode encoder for iPhone Control to generate, create GS1-128 image in iPhone applications.
www.OnBarcode.com
Code 39 Reader In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
LINQ to XML provides easy ways for adding, removing, and changing the content of an XML tree. These ways can be best seen with an example. Let s create an application like the one shown in Figure 13-8 that represents a data entry screen for the Employees.xml file.
Barcode Recognizer In VB.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Create QR Code JIS X 0510 In None
Using Barcode creator for Font Control to generate, create QR Code JIS X 0510 image in Font applications.
www.OnBarcode.com
Figure 13-8. Data entry screen for the Employees.xml file The application displays <employee> elements from the Employees.xml file. You can navigate among various employees using the VCR controls provided at the bottom. You can also jump to an employee with a specific employee ID using the combo box. The Add, Update, and Delete buttons add, change, or remove an <employee> element from the underlying XML tree, respectively. The Save button saves the modified XML tree back to the Employees.xml file.
Code 39 Encoder In Java
Using Barcode printer for Eclipse BIRT Control to generate, create Code 39 Extended image in BIRT reports applications.
www.OnBarcode.com
Creating EAN / UCC - 14 In Java
Using Barcode maker for Java Control to generate, create UCC.EAN - 128 image in Java applications.
www.OnBarcode.com
Loading the XML Document
When the application starts, it loads the Employees.xml file into an instance of the XElement class. This is done in the Load event of the form (see Listing 13-11). Listing 13-11. Loading an XML Document XElement doc = null; private void Form1_Load(object sender, EventArgs e) { doc=XElement.Load(Application.StartupPath + @"\employees.xml"); var result=from item in doc.Descendants("employee") select item.Attribute("employeeid").Value;
C HA P TER 13 WO RK ING W IT H LI NQ T O XM L
foreach (var obj in result) { comboBox1.Items.Add(obj); } FillControls(); } The code declares a variable of type XElement at form level. In the Load event, it loads Employees.xml into it. The descendent elements of the root node (i.e., all the <employee> elements) are retrieved using the Descendents() method, and their attributes are added to the combo box. The FillControls() method (discussed later) simply fetches the <employee> element matching the currently selected employee ID and displays its details (first name, last name, home phone, and notes) in the appropriate text boxes.
Copyright © OnBarcode.com . All rights reserved.