使用linq将xml转换成实体

// 1.实体类   
 class Address
    {
        public string Name { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
    }           

// 2.测试转换
  static void XmlTest()
        {
            string xmlStr = @"<root>
                                <Address Type='Billing'>  <Name> Tai Yee </Name>
                                     <Street> 8 Oak Avenue</Street >      
                                        <City> Old Town </City >         
                                           <State> PA </State >         
                                           <Zip> 95819 </Zip >         
                                           <Country> USA </Country >
                                         </Address> 
                                <Address Type='Billing'>  <Name> Tai Yee </Name>
                                     <Street> 8 Oak Avenue</Street >      
                                        <City> Old Town </City >         
                                           <State> PA </State >         
                                           <Zip> 95819 </Zip >         
                                           <Country> USA </Country >
                                         </Address>
                                <Address Type='Billing'>  <Name> Tai Yee </Name>
                                     <Street> 8 Oak Avenue</Street >      
                                        <City> Old Town </City >         
                                           <State> PA </State >         
                                           <Zip> 95819 </Zip >         
                                           <Country> USA </Country >
                                         </Address>
                                <Address Type='Billing'>  <Name> Tai Yee </Name>
                                     <Street> 8 Oak Avenue</Street >      
                                        <City> Old Town </City >         
                                           <State> PA </State >         
                                           <Zip> 95819 </Zip >         
                                           <Country> USA </Country >
                                         </Address>
                            </root>";

            XDocument doc = XDocument.Parse(xmlStr);
            List<Address> result = (from x in doc.Element("root").Elements("Address")
                          select new Address
                          {
                              Name = x.Element("Name").Value,
                              Street = x.Element("Street").Value,
                              City = x.Element("City").Value
                          }).ToList();

        }


MSDN详解及示例:
        https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-project-an-object-graph
posted @ 2017-07-13 10:05  Coder_小菜  阅读(689)  评论(0编辑  收藏  举报