LINQ:Language Integrated Query
使用LINQ to XML创建xml文件,实例代码如下
需要using System.Xml.Linq;
- XDocument xdoc = new XDocument( //XmlDocument 对象
- new XElement("customers", //XmlElement 对象
- new XElement("customer",
- new XAttribute("ID", "A"), //XmlAttribute 特性
- 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)
- )
- )
- )
- );
- Console.WriteLine(xdoc);
如果要处理Xml片段,只需要把XDocument xdoc = new XDocument改变成XElementx doc = new XElement即可处理Xml片段

本文介绍如何使用LINQ to XML库在C#中创建XML文件。通过实例代码展示了如何构建XML文档结构,包括元素、属性和子元素。适用于需要处理XML数据的开发者。
1437

被折叠的 条评论
为什么被折叠?



