Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
As shown in previous chapters, one of the themes in C# is easier construction of objects, with features such as object initializers and anonymous types. LINQ to XML continues this theme by introducing a new, easier way to create XML documents called functional construction in which the constructor calls can be nested in a way that naturally reflects the structure of the XML document. In the following Try It Out, you use functional constructors to make a simple XML document containing customers and orders.
TRY IT OUT: LINQ to XML ConstructorsFollow these steps to create the example in Visual Studio 2010:
The XML document shown on the output screen contains a very simplified version of the customer/order data you have seen in previous examples. Note that the root element of the XML document is <customers>, which contains two nested <customer> elements. These in turn contain a number of nested <order> elements. The <customer> elements have two attributes, <City> and <Region>, and the <order> elements have <Item> and <Price> attributes. Press Enter/Return to exit the program and make the console screen disappear. If you used Ctrl+F5 (Start Without Debugging), you may need to press Enter/Return twice. How It Works The first step is to reference the System.Xml.Linq namespace. All of the following examples in this chapter require that you add this line to your program: using System.Xml.Linq; While the System.Linq namespace is included by default when you create a project, the System.Xml.Linq namespace is not included; you must add this line explicitly. Next are the calls to the LINQ to XML constructors XDocument(), XElement(), and XAttribute(), which are nested inside one another as shown here: XDocument xdoc = new XDocument(
new XElement("customers",
new XElement("customer",
new XAttribute("ID", "A"),
.Note that the code here looks like the XML itself, where the document contains elements, and each element contains attributes and other elements. Let's look at each of these constructors in turn:
Finishing up the explanation of the first example, you add two child "order" elements to the "customer" element following the "ID", "City" and "Region" attributes: new XElement("order",
new XAttribute("Item", "Widget"),
new XAttribute("Price", 100)
),
new XElement("order",
new XAttribute("Item", "Tire"),
new XAttribute("Price", 200)
)These order elements have "Item" and "Price" attributes but no other children. Next, you display the contents of the XDocument to the console screen: Console.WriteLine(xdoc); This prints out the text of the XML document using the default ToString() method of XDocument(). Finally, you pause the screen so you can see the console output, and then wait until the user presses Enter: Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();After that your program exits the Main() method, which ends the program. |