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片段