c# code 39 generator Navigating an XML Document by Using XPathNavigator in Visual C#.NET

Encoder Code 39 Extended in Visual C#.NET Navigating an XML Document by Using XPathNavigator

Navigating an XML Document by Using XPathNavigator
Creating Code 39 Full ASCII In Visual C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create ANSI/AIM Code 39 image in .NET applications.
www.OnBarcode.com
Recognize ANSI/AIM Code 39 In Visual C#.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
In the previous section, you learned to create XPathNavigator from XmlDocument and XPathDocument. In this section, you will see how to use XPathNavigator and access various attributes and elements. To work through this section, you need to create a Windows Forms application like the one shown in Figure 4-2.
Creating Data Matrix ECC200 In C#
Using Barcode encoder for Visual Studio .NET Control to generate, create ECC200 image in VS .NET applications.
www.OnBarcode.com
GTIN - 12 Generation In Visual C#.NET
Using Barcode generator for VS .NET Control to generate, create UPC Code image in Visual Studio .NET applications.
www.OnBarcode.com
Figure 4-2. Navigating through XPathNavigator The application consists of a TreeView and a button. After you click the Load Tree button, the TreeView is populated with employee information from the Employees.xml file. The Click event handler of the Load Tree button contains the code shown in Listing 4-2.
Generating GS1 - 13 In Visual C#
Using Barcode printer for Visual Studio .NET Control to generate, create EAN-13 image in .NET applications.
www.OnBarcode.com
Printing ANSI/AIM Code 128 In C#
Using Barcode creation for .NET Control to generate, create Code128 image in .NET framework applications.
www.OnBarcode.com
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
Barcode Creator In C#.NET
Using Barcode drawer for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
ISBN - 13 Printer In Visual C#
Using Barcode generation for VS .NET Control to generate, create International Standard Book Number image in VS .NET applications.
www.OnBarcode.com
Listing 4-2. Navigating by Using XPathNavigator private void button1_Click(object sender, EventArgs e) { XPathDocument doc = new XPathDocument(Application.StartupPath + @"\employees.xml"); XPathNavigator navigator = doc.CreateNavigator(); navigator.MoveToRoot(); navigator.MoveToFirstChild(); TreeNode root = treeView1.Nodes.Add("Employees"); while (navigator.MoveToNext()) { if (navigator.HasChildren) { navigator.MoveToFirstChild(); do { string id = navigator.GetAttribute("employeeid", ""); TreeNode empnode = new TreeNode("Employee ID :" + id); root.Nodes.Add(empnode); navigator.MoveToFirstChild(); do { string name = navigator.Name; TreeNode node = new TreeNode(name + " : " + navigator.Value); empnode.Nodes.Add(node); } while (navigator.MoveToNext()); navigator.MoveToParent(); } while (navigator.MoveToNext()); } } }
ANSI/AIM Code 39 Creator In Java
Using Barcode generation for Android Control to generate, create Code 3 of 9 image in Android applications.
www.OnBarcode.com
Create Code 3 Of 9 In Objective-C
Using Barcode generation for iPad Control to generate, create Code39 image in iPad applications.
www.OnBarcode.com
CHAPTER 4 AC CES SING XML DOCUM ENTS BY USING THE XPATH DA TA MODEL
Generating EAN / UCC - 13 In Objective-C
Using Barcode printer for iPhone Control to generate, create EAN / UCC - 13 image in iPhone applications.
www.OnBarcode.com
Making QR Code ISO/IEC18004 In None
Using Barcode maker for Software Control to generate, create QR image in Software applications.
www.OnBarcode.com
The code begins by creating an instance of the XPathDocument class by passing the path of the XML file to its constructor. It then creates an XPathNavigator by calling the CreateNavigator() method of the XPathDocument class. We need to iterate through the document from the root and hence we call the MoveToRoot() method of XPathNavigator. This method moves the cursor of the XPathNavigator to the root of the document. Note that here the root of the document is the node that contains the entire tree of nodes. Because we want to start the iteration from the <employees> node, we call the MoveToFirstChild() method. Calling this method will place the navigator cursor at the <employees> node. A root node of the TreeView is then added. Next there are three loops. The outermost loop iterates through all the child nodes of the root node. In our case, this loop will be executed just once, because there is only one <employees> node. The second loop iterates through all the <employee> nodes, whereas the innermost loop iterates through the child nodes of the <employee> node, that is, the <firstname>, <lastname>, <homephone>, and <notes> nodes. The outermost loop uses the MoveToNext() method of the XPathNavigator class to advance the cursor onto the next node. It then decides whether there are any <employee> nodes using the HasChildren property. The HasChildren property returns true if there are child nodes to the current node; otherwise, it returns false. If there are <employee> nodes, the cursor is moved to the first <employee> node by calling the MoveToFirstChild() method, which moves the navigator cursor to the first child node. Now the code starts iterating through all the <employee> nodes. With each iteration, the value of the employee attribute is retrieved by using the GetAttribute() method. This method accepts two parameters: the name of the attribute to retrieve and the attribute namespace. Because our document does not contain any namespaces, an empty string is passed as the second parameter. A TreeView node is added for that employee ID. The cursor is then moved to the first child node of the <employee> node by using the MoveToFirstChild() method we discussed earlier. After this call, the cursor will be on the <firstname> node. Now the innermost loop starts. With each iteration, the name of the node is retrieved by using the Name property, and the value of the node is retrieved by using the Value property. The same process is carried out for all the child nodes, that is, <firstname>, <lastname>, <homephone>, and <notes>. After the innermost loop is finished, the navigator cursor is moved back to the parent <employee> node. This is done with the help of the MoveToParent() method, which moves the cursor pointer to the parent node of the current node. The same process is repeated for the remaining <employee> nodes.
Painting Barcode In Objective-C
Using Barcode encoder for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
USS Code 128 Drawer In None
Using Barcode generation for Online Control to generate, create ANSI/AIM Code 128 image in Online applications.
www.OnBarcode.com
Recognizing EAN / UCC - 13 In Visual C#.NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Recognize Barcode In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Print Data Matrix In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
www.OnBarcode.com
Create UPC-A Supplement 5 In Java
Using Barcode drawer for Java Control to generate, create GS1 - 12 image in Java applications.
www.OnBarcode.com
Print QR Code JIS X 0510 In Java
Using Barcode drawer for Eclipse BIRT Control to generate, create Quick Response Code image in BIRT applications.
www.OnBarcode.com
Drawing Code 128C In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create Code 128B image in .NET framework applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.