开发者

c# return all specific elements from an xml

Greetings. Im having a small trouble i would like to have some help with. Im having a very large xml file with about 1000 customers with diffrent customer information. And I would like to do methods to retrive this information. Ive been searching everywhere but cant seem to find what im looking for. Currently im trying:

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)??   
}
开发者_运维问答

(All elements) is where id like to retrive all the information. Using FirstOrDefault will only retrive the first element and LastOrDefault will only retrive the first element. If some one could help me i would be very greatefull.


you want a list of customers. Change the return value to IEnumerable and transform the query to IEnumerable with ToList()/ToArray():

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜