天天看點

Linq to XML說法——(一)建立,添加,查詢

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("暫無聯系方式");

        case "opdate":

               e1.SetValue("2009-12-21");

        }

    }

}

(六)儲存檔案

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"),

                           };

部落格園大道至簡

<a href="http://www.cnblogs.com/jams742003/" target="_blank">http://www.cnblogs.com/jams742003/</a>

轉載請注明:部落格園