linq读取xml

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结构如下:



  
    ALFKI
    Alfreds Futterkiste
    
Obere Str. 57
Berlin 12209 Germany 030-0074321 030-0076545 10643 1997-08-25T00:00:00 814.50 10692 1997-10-03T00:00:00 878.00 10702 1997-10-13T00:00:00 330.00 10835 1998-01-15T00:00:00 845.80 10952 1998-03-16T00:00:00 471.20 11011 1998-04-09T00:00:00 933.50
....

你可能感兴趣的:(Linq)