C# c从xml返回所有特定元素

C# c从xml返回所有特定元素,c#,xml,linq,ienumerable,C#,Xml,Linq,Ienumerable,你好。我遇到了一个小麻烦,我想得到一些帮助。我有一个非常大的xml文件,大约有1000个客户,他们有不同的客户信息。我想做一些方法来检索这些信息。我到处找,但似乎找不到我要找的东西。目前我正在尝试: public custInformation getcustInfo(string file) { //Load the xml file var xe = XDocument.Load(_server.MapPath(file)).Root; //Get in

你好。我遇到了一个小麻烦,我想得到一些帮助。我有一个非常大的xml文件,大约有1000个客户,他们有不同的客户信息。我想做一些方法来检索这些信息。我到处找,但似乎找不到我要找的东西。目前我正在尝试:

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)??   
}

所有元素都是我想要检索所有信息的地方。使用FirstOrDefault将仅检索第一个元素,而LastOrDefault将仅检索第一个元素。如果有人能帮我,我会非常忙。

你想要一份客户名单。将返回值更改为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();
}