- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
vb.net barcode generator open source LINQ TO XML in Visual C#
CHAPTER 29 LINQ TO XML Data Matrix Creation In C# Using Barcode creation for .NET framework Control to generate, create Data Matrix 2d barcode image in VS .NET applications. www.OnBarcode.comData Matrix 2d Barcode Scanner In Visual C#.NET Using Barcode recognizer for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.com<Fruit> <Name>Orange</Name> <Size>Large</Size> </Fruit> Press enter to finish We have created the parent XElement and added two children, and with very little effort we have a simple XML document. I have created the children and then added them to the parent, but I could have done everything in a single statement, like this: XElement myFruit = new XElement("Fruit", new XElement("Name", "Orange"), new XElement("Size", "Large")); This way of creating elements is very important when we start to use LINQ queries to generate XML in the Creating XML from LINQ Queries section later in this chapter. EAN / UCC - 13 Creator In Visual C# Using Barcode drawer for .NET Control to generate, create EAN 13 image in .NET applications. www.OnBarcode.comEncoding Linear 1D Barcode In C# Using Barcode creator for VS .NET Control to generate, create Linear image in VS .NET applications. www.OnBarcode.comUsing Arbitrary Types to Create XElements
QR Code ISO/IEC18004 Encoder In Visual C#.NET Using Barcode drawer for VS .NET Control to generate, create QR Code image in .NET applications. www.OnBarcode.comPDF-417 2d Barcode Creator In Visual C# Using Barcode generation for Visual Studio .NET Control to generate, create PDF417 image in Visual Studio .NET applications. www.OnBarcode.comIn Listings 29-1 and 29-2, I created the child XElements by passing two string parameters to the constructor: the name of the element and the value. In fact, you can pass any object as the second parameter, and a string representation of the object will be included in the XML data. Listing 29-3 demonstrates this using an enum and a custom class. Listing 29-3. Using String Representations of Objects in XElements using System; using System.Xml.Linq; enum FruitSize { Small, Medium, Large } class Fruit { public Fruit(string nameParam, int stockParam) { Name = nameParam; ItemsInStock = stockParam; } public string Name { get; set; } public int ItemsInStock { get; set; } public override string ToString() { return Name; } } class Listing 03 { GS1-128 Creator In Visual C# Using Barcode creator for .NET framework Control to generate, create GS1 128 image in VS .NET applications. www.OnBarcode.comOneCode Generator In Visual C# Using Barcode maker for Visual Studio .NET Control to generate, create USPS OneCode Solution Barcode image in .NET framework applications. www.OnBarcode.comCHAPTER 29 LINQ TO XML
Data Matrix ECC200 Generator In None Using Barcode encoder for Online Control to generate, create ECC200 image in Online applications. www.OnBarcode.comECC200 Drawer In VB.NET Using Barcode encoder for .NET framework Control to generate, create Data Matrix image in VS .NET applications. www.OnBarcode.comstatic void Main(string[] args) { // create a Fruit object Fruit myFruitObject = new Fruit("Orange", 200); // create XElements using the Fruit object XElement myNameElement = new XElement("Name", myFruitObject); XElement mySizeElement = new XElement("Size", FruitSize.Large); // create a hierarchy of XElements XElement myFruitElement = new XElement("Fruit", myNameElement, mySizeElement); // print out the XElement object Console.WriteLine(myFruitElement); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } In Listing 29-3, I pass a Fruit object as the parameter to create one XElement, which includes the result of the ToString method in the XML. The other XElement is created using a value from an enum, which is expressed as a helpful string value. Compiling and running Listing 29-3 produces the following output: <Fruit> <Name>Orange</Name> <Size>Large</Size> </Fruit> Press enter to finish Notice that only the result from the ToString method of the Fruit object is included in the XML. The Fruit class defines a property called ItemsInStock that is ignored. If you pass an object to the constructor of XElement like this, you must be sure that the ToString method returns all the information you need included in the XML. As an alternative, you can create XElements to express the value or result of individual members, as demonstrated in Listing 29-4. Listing 29-4. Creating Multiple XElements from the Members of an Object using System; using System.Xml.Linq; class Listing 04 { static void Main(string[] args) { // create a Fruit object Fruit myFruitObject = new Fruit("Orange", 200); Creating EAN / UCC - 14 In Visual Basic .NET Using Barcode creator for .NET framework Control to generate, create EAN / UCC - 14 image in .NET applications. www.OnBarcode.comEAN-13 Reader In None Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comCHAPTER 29 LINQ TO XML
Creating GTIN - 12 In Objective-C Using Barcode creation for iPad Control to generate, create UPC-A Supplement 5 image in iPad applications. www.OnBarcode.comGS1 DataBar Creator In Java Using Barcode creation for Java Control to generate, create GS1 DataBar Limited image in Java applications. www.OnBarcode.com// create XElements using the Fruit object XElement myNameElement = new XElement("Name", myFruitObject.Name); XElement myStockElement = new XElement("ItemsInStock", myFruitObject.ItemsInStock); XElement mySizeElement = new XElement("Size", FruitSize.Large); // create a hierarchy of XElements XElement myFruitElement = new XElement("Fruit", myNameElement, myStockElement, mySizeElement); // print out the XElement object Console.WriteLine(myFruitElement); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } Listing 29-4 explicitly creates XElements for both of the properties of the Fruit object. Compiling and running Listing 29-4 produces the following result: <Fruit> <Name>Orange</Name> <ItemsInStock>200</ItemsInStock> <Size>Large</Size> </Fruit> Press enter to finish Creating Data Matrix In Java Using Barcode creation for Java Control to generate, create DataMatrix image in Java applications. www.OnBarcode.comData Matrix 2d Barcode Generator In .NET Using Barcode generation for Reporting Service Control to generate, create Data Matrix 2d barcode image in Reporting Service applications. www.OnBarcode.comCreating Other XML Node Types
UPC - 13 Generation In Java Using Barcode generator for Eclipse BIRT Control to generate, create EAN 13 image in Eclipse BIRT applications. www.OnBarcode.comPDF-417 2d Barcode Generation In Visual Studio .NET Using Barcode generation for .NET framework Control to generate, create PDF417 image in .NET framework applications. www.OnBarcode.comXElement isn t the only kind of XML node available. There are classes that you can use to create different node types. Listing 29-5 provides an example. Listing 29-5. Creating Different XML Node Types using System; using System.Xml.Linq; class Listing 05 { static void Main(string[] args) { // create an XElement XElement myElement = new XElement("ElementName", "ElementValue"); // create an attribute XAttribute myAttribute = new XAttribute("AttributeName", "AttributeValue"); GS1 128 Generator In Java Using Barcode generation for Java Control to generate, create EAN128 image in Java applications. www.OnBarcode.comGenerate Code 3 Of 9 In None Using Barcode generation for Office Excel Control to generate, create Code-39 image in Office Excel applications. www.OnBarcode.com |
|