zoukankan      html  css  js  c++  java
  • Linq:从XML获取数据

    实体类

     1 public class Customer
     2         {
     3             public string CustomerID { get; set; }
     4             public string CompanyName { get; set; }
     5             public string Address { get; set; }
     6             public string City { get; set; }
     7             public string Region { get; set; }
     8             public string PostalCode { get; set; }
     9             public string Country { get; set; }
    10             public string Phone { get; set; }
    11             public string Fax { get; set; }
    12             public Order[] Orders { get; set; }
    13        public int CId{ get; set; }

    14 }


    从XML中获取数据

     1  public class LinqSamples
     2  {
     3               
    4
    public List<Customer> GetList() 5 { 6 List<Customer> customerList = ( 8 from e in XDocument.Load("Customers.xml"). 9 Root.Elements("customer") 10 select new Customer 11 { 12 CustomerID = (string)e.Element("id"), 13 CompanyName = (string)e.Element("name"), 14 Address = (string)e.Element("address"), 15 City = (string)e.Element("city"), 16 CId= (int)e.Element("cid"),
    17 PostalCode = (string)e.Element("postalcode"), 18 Country = (string)e.Element("country"), 19 Phone = (string)e.Element("phone"), 20 Fax = (string)e.Element("fax"), 21 Orders = ( 22 from o in e.Elements("orders").Elements("order") 23 select new Order 24 { 25 OrderID = (int)o.Element("id"), 26 OrderDate = (DateTime)o.Element("orderdate"), 27 Total = (decimal)o.Element("total") 28 }) 29 .ToArray() 30 }) 31 .ToList(); 32             return customerList;
    33 } 34 }

    XML数据文件

     1 <?xml version="1.0"?>
     2 <customers>
     3   <customer>
        <cid>1</cid>
    4 <id>ALFKI</id> 5 <name>Alfreds Futterkiste</name> 6 <address>Obere Str. 57</address> 7 <city>Berlin</city> 8 <postalcode>12209</postalcode> 9 <country>Germany</country> 10 <phone>030-0074321</phone> 11 <fax>030-0076545</fax> 12 <orders> 13 <order> 14 <id>10643</id> 15 <orderdate>1997-08-25T00:00:00</orderdate> 16 <total>814.50</total> 17 </order> 18 <order> 19 <id>10692</id> 20 <orderdate>1997-10-03T00:00:00</orderdate> 21 <total>878.00</total> 22 </order> 23 <order> 24 <id>10702</id> 25 <orderdate>1997-10-13T00:00:00</orderdate> 26 <total>330.00</total> 27 </order> 28 <order> 29 <id>10835</id> 30 <orderdate>1998-01-15T00:00:00</orderdate> 31 <total>845.80</total> 32 </order> 33 <order> 34 <id>10952</id> 35 <orderdate>1998-03-16T00:00:00</orderdate> 36 <total>471.20</total> 37 </order> 38 <order> 39 <id>11011</id> 40 <orderdate>1998-04-09T00:00:00</orderdate> 41 <total>933.50</total> 42 </order> 43 </orders> 44 </customer> 45 <customer>     <cid>2</cid>
    46 <id>ANATR</id> 47 <name>Ana Trujillo Emparedados y helados</name> 48 <address>Avda. de la Constitución 2222</address> 49 <city>México D.F.</city> 50 <postalcode>05021</postalcode> 51 <country>Mexico</country> 52 <phone>(5) 555-4729</phone> 53 <fax>(5) 555-3745</fax> 54 <orders> 55 <order> 56 <id>10308</id> 57 <orderdate>1996-09-18T00:00:00</orderdate> 58 <total>88.80</total> 59 </order> 60 <order> 61 <id>10625</id> 62 <orderdate>1997-08-08T00:00:00</orderdate> 63 <total>479.75</total> 64 </order> 65 <order> 66 <id>10759</id> 67 <orderdate>1997-11-28T00:00:00</orderdate> 68 <total>320.00</total> 69 </order> 70 <order> 71 <id>10926</id> 72 <orderdate>1998-03-04T00:00:00</orderdate> 73 <total>514.40</total> 74 </order> 75 </orders> 76 </customer> 77 </customers>

     内容源自:http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

  • 相关阅读:
    解决object at 0x01DB75F0
    github导入文件操作
    git出现: not a git repository
    scrapy框架爬取妹子图片
    mysql触发器,视图,游标
    mysql锁
    在k-means或kNN,我们是用欧氏距离来计算最近的邻居之间的距离。为什么不用曼哈顿距离?
    数据库存储,索引,事务常见问题
    使用Request+正则抓取猫眼电影(常见问题)
    Tensorflow()
  • 原文地址:https://www.cnblogs.com/yf2011/p/3369927.html
Copyright © 2011-2022 走看看