c# code 39 barcode generator Opening an Existing XML Document for Parsing in C#.NET

Drawer Code 3 of 9 in C#.NET Opening an Existing XML Document for Parsing

Opening an Existing XML Document for Parsing
Code 39 Extended Creation In Visual C#
Using Barcode maker for .NET Control to generate, create USS Code 39 image in Visual Studio .NET applications.
www.OnBarcode.com
Recognizing Code 3/9 In C#
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
While working with classic XML DOM, you used the XmlDocument class to load an XML document. LINQ to XML offers two classes to load XML data: XDocument and XElement. In most situations, XElement is all you need to load XML files, streams, and fragments. However, in some rare cases, you may need to use XDocument instead. These special cases include the following: You want to add comments at the top level. You want to add processing instructions at the top level. You want to use a DTD for your XML document.
Barcode Generation In C#.NET
Using Barcode printer for VS .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
1D Barcode Maker In C#
Using Barcode creator for VS .NET Control to generate, create Linear image in .NET applications.
www.OnBarcode.com
C HA P TER 13 WO RK ING W IT H LI NQ T O XM L
Generating ECC200 In C#.NET
Using Barcode creation for VS .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Generator In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
In any case, you can load XML data from a URI, a TextReader, an XmlReader, or even an XML string. To demonstrate the use of XElement in loading XML data, let s build an application like the one shown in Figure 13-4.
Drawing GTIN - 13 In C#.NET
Using Barcode maker for .NET Control to generate, create GTIN - 13 image in .NET applications.
www.OnBarcode.com
Identcode Drawer In C#
Using Barcode maker for .NET framework Control to generate, create Identcode image in Visual Studio .NET applications.
www.OnBarcode.com
Figure 13-4. Application that loads XML data in XElement The application consists of four radio buttons for selecting the place from which the XML data is to be loaded. The TextBox allows you to specify a URL where the XML file is located or raw XML markup. Clicking the Load button loads the data in an XElement and displays a message box with a success message. The Click event handler of the Load button is shown in Listing 13-7. Listing 13-7. Loading XML Data Using XElement private void button1_Click(object sender, EventArgs e) { try { XElement root = null; if (radioButton1.Checked) { root = XElement.Load(textBox1.Text); } if (radioButton2.Checked) { StreamReader reader = File.OpenText(textBox1.Text); root = XElement.Load(reader); }
Painting ANSI/AIM Code 39 In Java
Using Barcode maker for Android Control to generate, create Code 39 image in Android applications.
www.OnBarcode.com
Encoding Code 3 Of 9 In C#.NET
Using Barcode creator for Visual Studio .NET Control to generate, create Code-39 image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 13 WORKING WITH LINQ TO XML
PDF417 Drawer In VS .NET
Using Barcode generation for Reporting Service Control to generate, create PDF417 image in Reporting Service applications.
www.OnBarcode.com
USS Code 39 Decoder In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
if (radioButton3.Checked) { XmlReader reader = XmlReader.Create(textBox1.Text); root = XElement.Load(reader); } if (radioButton4.Checked) { root = XElement.Parse(textBox1.Text); } MessageBox.Show("XML Data Loaded Successfully!"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } The code consists of a series of if blocks for checking the selected radio button. The first three if blocks use the Load() method of the XElement class. Notice that Load() is a static method of the XElement class and accepts a URI, a TextReader, or an XmlReader as a source of XML data. If the XML data is in the form of a raw string, the Parse() method is used instead of Load(). The Load() and Parse() methods return an instance of the XElement class that can be used for further processing.
UCC - 12 Creation In Java
Using Barcode encoder for Java Control to generate, create EAN / UCC - 14 image in Java applications.
www.OnBarcode.com
Code 128 Creation In None
Using Barcode creation for Office Word Control to generate, create Code 128 image in Office Word applications.
www.OnBarcode.com
Note In order to compile the code from Listing 13-7, you must import the System.IO and System.Xml.Linq
Making Data Matrix In Visual Studio .NET
Using Barcode generation for Reporting Service Control to generate, create Data Matrix 2d barcode image in Reporting Service applications.
www.OnBarcode.com
Draw QR Code JIS X 0510 In None
Using Barcode generator for Excel Control to generate, create QR Code image in Microsoft Excel applications.
www.OnBarcode.com
namespaces. This applies to most of the examples illustrated in this chapter.
Decoding GS1 - 12 In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Drawing ECC200 In Objective-C
Using Barcode maker for iPad Control to generate, create DataMatrix image in iPad applications.
www.OnBarcode.com
To test the application, you can use the same Employees.xml file that we created in 2 (see Listing 2-2 for the complete XML markup).
Painting GTIN - 128 In Java
Using Barcode maker for Android Control to generate, create UCC - 12 image in Android applications.
www.OnBarcode.com
Code 128 Code Set B Encoder In None
Using Barcode creation for Excel Control to generate, create ANSI/AIM Code 128 image in Microsoft Excel applications.
www.OnBarcode.com
Navigating Through an XML Tree
Under LINQ to XML, all the child nodes of an element can be accessed via the Nodes() method. The Nodes() method returns a sequence of IEnumerable<XNode> that can be iterated to access the individual nodes. If your interest is purely in elements, you can use the Elements() method that returns a sequence of IEnumerable<XElement>. The FirstNode and LastNode properties allow you to access the first and the last nodes, respectively. Similarly, the descendents can be accessed via the Descendents() method. To look at many of these methods in action, we will develop an application that populates a TreeView control with data from the Employees.xml file. The application user interface is shown in Figure 13-5.
C HA P TER 13 WO RK ING W IT H LI NQ T O XM L
Figure 13-5. Navigating through an XML tree using LINQ to XML The application consists of a TreeView control for displaying several <employee> elements. Each employee tree node further displays information about that employee. The actual code to populate the tree view goes in the Click event handler of the Load Tree button (see Listing 13-8). Listing 13-8. Loading the Tree with Employee Details private void button1_Click(object sender, EventArgs e) { XElement root = XElement.Load(Application.StartupPath + @"\Employees.xml"); TreeNode rootNode = new TreeNode(root.Name.LocalName); treeView1.Nodes.Add(rootNode); foreach(XElement employee in root.Elements()) { TreeNode employeeNode = new TreeNode("Employee ID :" + employee.Attribute("employeeid").Value); rootNode.Nodes.Add(employeeNode); if (employee.HasElements) { foreach(XElement employeechild in employee.Descendants()) { TreeNode childNode = new TreeNode(employeechild.Value); employeeNode.Nodes.Add(childNode); } } } } The code first loads the Employees.xml file into an instance of the XElement class. The Name property of the XElement class is of type XName and represents the name of the element. The
Copyright © OnBarcode.com . All rights reserved.