开发者

Using LINQ to XML to build a collection - only getting the top item for some reason

开发者 https://www.devze.com 2022-12-12 06:14 出处:网络
So I\'m trying to parse some XML from the twitter API and for some reason the LINQ query below only returns 1 row.When I step through the c开发者_JAVA百科ode to view the raw XML it\'s the usual 20+ it

So I'm trying to parse some XML from the twitter API and for some reason the LINQ query below only returns 1 row. When I step through the c开发者_JAVA百科ode to view the raw XML it's the usual 20+ items so I'm not sure what I'm doing wrong here.

Any help for a LINQ to XML newbie?

            List<TwitterStatus> StatusCollection = new List<TwitterStatus>();
            XDocument xdoc = XDocument.Parse(xmldata);

            StatusCollection = (from status in xdoc.Descendants("statuses")
                                select new TwitterStatus
                                {
                                    Text = status.Element("status").Element("text").Value,
                                    User = status.Element("status").Element("user").Element("screen_name").Value
                                }).ToList();


Now I dont know the form of twitter XML, but could it be that the element "statuses", contains multiple "status" elements. So Desecendants("statuses") only find one element. Would you not need to do

StatusCollection = (from status in xdoc.Descendants("status") select new TwitterStatus {
Text = status.Element("text").Value,
User = status.Element("user").Element("screen_name").Value }).ToList();

0

精彩评论

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