代码
XDocument xdoc = new XDocument(
new XElement("customers",
new XElement("customer",
new XAttribute("ID", "A"),
new XAttribute("City", "New York"),
new XAttribute("Region", "North America"),
new XElement("order",
new XAttribute("Item", "Widget"),
new XAttribute("Price", 100)
),
new XElement("order",
new XAttribute("Item", "Tire"),
new XAttribute("Price", 200)
)
),
new XElement("customer",
new XAttribute("ID", "B"),
new XAttribute("City", "Mumbai"),
new XAttribute("Region", "Asia"),
new XElement("order",
new XAttribute("Item", "Oven"),
new XAttribute("Price", 501)
)
)
)
);
string xmlFileName = @"c:\BegVCSharp\Chapter29\Xml\example2.xml";
xdoc.Save(xmlFileName);
XDocument xdoc2 = XDocument.Load(xmlFileName);
代码
XElement xdoc = XElement.Parse(@"
<customers>
<customer ID=""A"" City=""New York"" Region=""North America"">
<order Item=""Widget"" Price=""100"" />
<order Item=""Tire"" Price=""200"" />
</customer>
<customer ID=""B"" City=""Mumbai"" Region=""Asia"">
<order Item=""Oven"" Price=""501"" />
</customer>
</customers>
");
Console.WriteLine("Contents of xdoc:");
Console.WriteLine(xdoc);