Create and save Xml documents using XML. Linq

Source: Internet
Author: User

 

Code

    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);

 

 

 

Code

  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);

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.