- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
c# code 39 generator CH APT ER 4 AC CE SSI NG XM L DO C UMENT S BY USI NG TH E XPAT H DA TA MO D EL in Visual C#.NET
CH APT ER 4 AC CE SSI NG XM L DO C UMENT S BY USI NG TH E XPAT H DA TA MO D EL Encoding Code 3 Of 9 In Visual C# Using Barcode generator for .NET framework Control to generate, create Code-39 image in VS .NET applications. www.OnBarcode.comReading Code 39 Extended In C#.NET Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comSelecting Nodes
Linear Generator In C#.NET Using Barcode maker for VS .NET Control to generate, create 1D Barcode image in Visual Studio .NET applications. www.OnBarcode.comQR-Code Maker In C# Using Barcode creation for VS .NET Control to generate, create QR image in Visual Studio .NET applications. www.OnBarcode.comThis chapter began with a brief overview of XPath and its vocabulary, including terms such as axis, node test, predicate, and function. You might be wondering where they come into the picture. It s time now to see those features in action. To test various XPath expressions, we will create a simple application that looks like the one shown in Figure 4-3. Generate ECC200 In Visual C# Using Barcode generation for VS .NET Control to generate, create Data Matrix image in .NET framework applications. www.OnBarcode.comPrinting EAN-13 In Visual C#.NET Using Barcode drawer for VS .NET Control to generate, create EAN-13 Supplement 5 image in VS .NET applications. www.OnBarcode.comFigure 4-3. Executing XPath expressions The application consists of a text box positioned at the top to enter XPath expressions. After you click the Execute button, the given expression is executed and its results are displayed in another text box at the bottom. The label at the bottom displays the total number of rows returned by the expression. Listing 4-3 shows the Click event handler of the Execute button. Listing 4-3. Using the Select() Method private void button1_Click(object sender, EventArgs e) { XPathDocument doc = new XPathDocument(Application.StartupPath + @"\employees.xml"); XPathNavigator navigator = doc.CreateNavigator(); XPathNodeIterator iterator = navigator.Select(textBox1.Text); try { label3.Text = "The expressions returned " + iterator.Count + " nodes"; if (iterator.Count > 0) { while (iterator.MoveNext()) Make USS-128 In Visual C#.NET Using Barcode creator for .NET framework Control to generate, create USS-128 image in .NET applications. www.OnBarcode.comUSPS Intelligent Mail Drawer In Visual C#.NET Using Barcode encoder for Visual Studio .NET Control to generate, create OneCode image in Visual Studio .NET applications. www.OnBarcode.comCHAPTER 4 AC CES SING XML DOCUM ENTS BY USING THE XPATH DA TA MODEL
Create Code39 In Java Using Barcode printer for BIRT Control to generate, create Code 39 image in BIRT applications. www.OnBarcode.comCode 3/9 Encoder In VS .NET Using Barcode maker for Reporting Service Control to generate, create ANSI/AIM Code 39 image in Reporting Service applications. www.OnBarcode.com{ textBox2.Text = iterator.Current.OuterXml; } } else { textBox2.Text = "No results"; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } The code creates an instance of XPathDocument by passing the path of the Employees.xml file. Then an XPathNavigator is obtained by using the CreateNavigator() method of XPathDocument. The code then calls the Select() method of XPathNavigator, which accepts an XPath expression and returns an instance of XPathNodeIterator. The XPathNodeIterator class allows you to iterate through the returned nodes and has a number of properties and methods to assist you. To start with, the Count property tells you how many nodes were selected by the Select() method. After you are satisfied that there were some results, you can iterate through the selected nodes by using the MoveNext() method. On each node, you then use the Current property to give you a reference to the XPathNavigator that is positioned at the current node. You can then call any of the methods and properties of XPathNavigator. In our example, we simply display the OuterXml property of the underlying XPathNavigator in a text box. Though not used in our example, the CurrentPosition property of XPathNodeIterator returns the current index of the node being accessed. Now let s try some XPath expressions by using our application. Some XPath expressions relevant to our XML document (Employees.xml) are given in Table 4-6. Table 4-6. Examples of XPath Expressions Encode EAN 128 In Java Using Barcode creator for Java Control to generate, create EAN / UCC - 13 image in Java applications. www.OnBarcode.comUCC.EAN - 128 Creator In Java Using Barcode creation for BIRT reports Control to generate, create EAN / UCC - 13 image in BIRT reports applications. www.OnBarcode.comPurpose
Barcode Maker In VS .NET Using Barcode encoder for ASP.NET Control to generate, create Barcode image in ASP.NET applications. www.OnBarcode.comCode 39 Extended Creation In VB.NET Using Barcode generator for VS .NET Control to generate, create USS Code 39 image in .NET applications. www.OnBarcode.comTo select an employee whose employee ID is 1 To select the employee whose first name is Andrew To select the last employee from the document To select the employee whose index is 2 To select an employee whose name contains Nancy To select the name of the first employee USS Code 128 Generation In .NET Framework Using Barcode generator for ASP.NET Control to generate, create Code 128 Code Set C image in ASP.NET applications. www.OnBarcode.comQR Code ISO/IEC18004 Generator In Java Using Barcode generator for BIRT reports Control to generate, create QR image in BIRT reports applications. www.OnBarcode.comExpression
EAN-13 Supplement 5 Generation In Java Using Barcode printer for Java Control to generate, create EAN13 image in Java applications. www.OnBarcode.comPaint EAN / UCC - 13 In Java Using Barcode printer for Android Control to generate, create UPC - 13 image in Android applications. www.OnBarcode.comemployees/employee[@employeeid=1] employees/employee[firstname/text()='Andrew'] employees/employee[last()] employees/employee[position()=2] employees/employee[contains(firstname,'Nancy')] employees/employee/firstname[text()] UPCA Maker In Visual Studio .NET Using Barcode drawer for ASP.NET Control to generate, create UPC Code image in ASP.NET applications. www.OnBarcode.comRecognizing GTIN - 128 In C# Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comCH APT ER 4 AC CE SSI NG XM L DO C UMENT S BY USI NG TH E XPAT H DA TA MO D EL
Selecting Single Nodes
The Select() method returns all the nodes that are obtained after evaluating the XPath expression. There is a method called SelectSingleNode() that executes the supplied XPath expression and returns an XPathNavigator object (and not an XPathNodeIterator) that contains the first matching node for the specified expression. You can then use the XPathNavigator object to navigate through the nodes. SelectSingleNode() comes in handy when you know that your XPath expression is going to return just one node. For example, in our document we can use SelectSingleNode() to extract an employee matching a specific employee ID. To illustrate the use of SelectSingleNode(), you need to develop an application like the one shown in Figure 4-4. Figure 4-4. Using the SelectSingleNode() method The application contains a text box to accept an employee ID and nine labels. Clicking the Show button displays details of an employee, and because the employee ID is unique in Employees.xml, we can safely use SelectSingleNode() here. Listing 4-4 shows the relevant code. Listing 4-4. Calling the SelectSingleNode() Method private void button1_Click(object sender, EventArgs e) { XPathDocument doc = new XPathDocument(Application.StartupPath + @"\employees.xml"); XPathNavigator navigator = doc.CreateNavigator(); XPathNavigator result = navigator.SelectSingleNode(@"employees/employee[@employeeid=" + textBox1.Text + "]"); result.MoveToFirstChild();
|
|