Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of elements in an XML document

I am wondering if it is possible to count the number of elements in an XML document preferably being able to fitler using something similar to where (string)query.Attribute("attName") == att.

Using the best of my knowledge i have tried the following but unfortunatly i can't seem to make it work.

                        var listElements = reader.Elements("shortlist");

                        foreach (var element in listElements)
                        {
                            XElement _xml;
                            location.Position = 0;
                            System.IO.StreamReader file = new System.IO.StreamReader(location);
                            _xml = XElement.Parse(file.ReadToEnd());
                            XAttribute attName = _xml.Attribute("attN");

                             if (attName.Value == att)
                            {
                                Count++;
                            }                              
                        }

Thanks!

like image 733
Jamie Avatar asked Nov 25 '10 19:11

Jamie


1 Answers

Given that doc is an instance of XDocument

doc.Root.Descendants().Count(d => (string)d.Attribute("attName") == "value");
like image 91
Matt Ellen Avatar answered Nov 14 '22 22:11

Matt Ellen