Linq创建,添加,查询XML实例
Linq Xml操作
场景:产品采购单。
描述:产品采购单用于描述产品的采购,它可以从各地进行采购,且每地可以采购多种商品。地址信息包括:城市,电话,联系人,日期,商品;商品包括0到多项,商品包括:产品名称,编号,描述,单价,采购总量。
<?xml version="1.0" encoding="utf-8" ?> <PurchaseOrder> <address> <city></city> <call></call> <contact></contact> <opdate></opdate> <products> <product> <name></name> <num></num> <price></price> <total></total> <description></description> </product> </products> </address> </PurchaseOrder>
(一)创建单子
创建声明
XDocument doc = new XDocument(); doc.Declaration = new XDeclaration("1.0","utf-8","no");
说明:Xdocument所以名字空间:System.Xml.Linq
(二)添加根元素
doc.Add(new XElement("purchaseOrder"));
(三)添加地址address元素
doc.Element("purchaseOrder").Add(new XElement("address")); doc.Element("purchaseOrder").Element("address"). Add( new XElement("city"), new XElement("call"), new XElement("contact"), new XElement("opdate"), new XElement("products") );
(四)Linq to xml添加产品
//定义产品列表 IList<product> _list = new List<product>() { new product{ name="产品1", num="B001", price=12,total=20,description="产品1描述"}, new product{ name="产品2", num="B002", price=16,total=22,description="产品2描述"} }; //添加产品 doc.Element("purchaseOrder").Element("address").Element("products").Add ( from q in _list select new XElement("product", new XElement("name",q.name), new XElement("num",q.num), new XElement("price", q.price), new XElement("total", q.total), new XElement("description", q.description) ));
(五)设置元素值
/
/为city,call,contact,opdate设置值 var _addressList = from q in doc.Elements("purchaseOrder").Elements("address") select q; foreach (XElement dd in _addressList) { foreach (XElement e1 in (from p in dd.Elements() select p)) { switch (e1.Name.ToString()) { case "city": e1.SetValue("石家庄"); break; case "call": e1.SetValue("86868666"); break; case "contact": e1.SetValue("暂无联系方式"); break; case "opdate": e1.SetValue("2009-12-21"); break; } } }
(六)保存文件
doc.Save(@"E:/test8/LinqTest/LToXml/source/PurchaseOrder.xml");
(七)在最后一个产品之后加一个新产品
doc.Element("purchaseOrder").Element("address").Element("products") .Add(new XElement("product", new XElement("name", "产品3"), new XElement("num", "C003"), new XElement("price", 18), new XElement("total", 108), new XElement("description", "产品3") ));
(八)在第一个产品这前添加一个新产品
doc.Element("purchaseOrder").Element("address").Element("products").AddFirst ( new XElement("product", new XElement("name", "产品4"), new XElement("num", "C004"), new XElement("price", 66), new XElement("total", 27), new XElement("description", "产品4")) );
(九)产品列表
//得到产品列表 var productList = from q in doc.Root .Element("address") .Element("products") .Elements("product") select q; //这个列表如下: /************************************************ Name num price total description 产品4 C004 66 27 产品4 产品1 B001 12 20 产品1描述 产品2 B002 16 22 产品2描述 产品3 C003 18 108 产品3 **************************************************/
(十)可以根据这个表进行linq查询
//查询产品总量 var iTotal = productList.Sum(p=>(int)p.Element("total")); //查询总价 var iTotalPrice = productList.Sum(q => (int)q.Element("price") * (int)q.Element("total")); //查询描述是"产品3"的产品 var product3 = from q in productList where (string)q.Element("description")== "产品3" select new product{ name=(string)q.Element("name"), num=(string)q.Element("num"), price=(int)q.Element("price"), total=(int)q.Element("total"), description=(string)q.Element("description"), };
加支付宝好友偷能量挖...