2011-04-01 34 views
0

问候。我有一个小麻烦,我想有一些帮助。我有一个非常大的xml文件,有大约1000个客户,有不同的客户信息。我想做一些方法来检索这些信息。我一直在寻找到处,但似乎无法找到即时寻找。目前我试图:c#从xml返回所有特定元素

public custInformation getcustInfo(string file) {  
    //Load the xml file 
    var xe = XDocument.Load(_server.MapPath(file)).Root; 

    //Get information 
    return (from p in xe.Descendants("cust-account").Descendants("cust-info") 
      select new custInformation 
      { 
       firstName = (string)p.Element("cust-fname"), 
       lastName = (string)p.Element("cust-lname"), 
       address = (string)p.Element("cust-address1"), 
      }).(All elements)?? 
} 

(所有元素)是在哪里id喜欢检索所有的信息。使用FirstOrDefault只会回顾第一个元素,而LastOrDefault只会回顾第一个元素。如果有人能帮助我,我会变得非常优秀。

回答

0

你想要一个客户名单。将返回值更改为IEnumerable ,并使用ToList()/ ToArray()将查询转换为IEnumerable:

public IEnumerable<custInformation> getcustInfo(string file) {  
    //Load the xml file 
    var xe = XDocument.Load(_server.MapPath(file)).Root; 

    //Get information 
    return (from p in xe.Descendants("cust-account").Descendants("cust-info") 
      select new custInformation 
      { 
       firstName = (string)p.Element("cust-fname"), 
       lastName = (string)p.Element("cust-lname"), 
       address = (string)p.Element("cust-address1"), 
      }).ToList(); 
} 
相关问题