加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

linq读取xml

发布时间:2020-12-15 22:30:29 所属栏目:百科 来源:网络整理
导读:var customerList = (from e in XDocument.Load("customers.xml").Root.Elements("customer") select new Customer { CustomerID = (string)e.Element("id"),CompanyName = (string)e.Element("name"),Address = (string)e.Element("address"),City = (strin
var customerList = (from e in XDocument.Load("customers.xml").Root.Elements("customer")
                                select new Customer
                                {
                                    CustomerID = (string)e.Element("id"),CompanyName = (string)e.Element("name"),Address = (string)e.Element("address"),City = (string)e.Element("city"),Region = (string)e.Element("region"),PostalCode = (string)e.Element("postalcode"),Country = (string)e.Element("country"),Phone = (string)e.Element("phone"),Fax = (string)e.Element("fax"),Orders = (
                                        from o in e.Elements("orders").Elements("order")
                                        select new Order
                                        {
                                            OrderID = (int)o.Element("id"),OrderDate = (DateTime)o.Element("orderdate"),Total = (decimal)o.Element("total")
                                        }).ToArray()
                                }).ToList();

xml结构如下:

<?xml version="1.0"?>
<customers>
  <customer>
    <id>ALFKI</id>
    <name>Alfreds Futterkiste</name>
    <address>Obere Str. 57</address>
    <city>Berlin</city>
    <postalcode>12209</postalcode>
    <country>Germany</country>
    <phone>030-0074321</phone>
    <fax>030-0076545</fax>
    <orders>
      <order>
        <id>10643</id>
        <orderdate>1997-08-25T00:00:00</orderdate>
        <total>814.50</total>
      </order>
      <order>
        <id>10692</id>
        <orderdate>1997-10-03T00:00:00</orderdate>
        <total>878.00</total>
      </order>
      <order>
        <id>10702</id>
        <orderdate>1997-10-13T00:00:00</orderdate>
        <total>330.00</total>
      </order>
      <order>
        <id>10835</id>
        <orderdate>1998-01-15T00:00:00</orderdate>
        <total>845.80</total>
      </order>
      <order>
        <id>10952</id>
        <orderdate>1998-03-16T00:00:00</orderdate>
        <total>471.20</total>
      </order>
      <order>
        <id>11011</id>
        <orderdate>1998-04-09T00:00:00</orderdate>
        <total>933.50</total>
      </order>
    </orders>
  </customer>
....

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读