Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
XML
<Model>
<Object type="Book">
<Component id="123">
<Element id="123">
<Article id="1">
</Article>
<Article id="2">
</Article>
</Element>
</Component>
</Object>
</Model>


In the above XMl file if at least one Article element is available
then the Object Should be counted.

Similarly how to count if the file has large lines.
But the structure should be the above on.

How to do this
Posted

1 solution

Best way would be using linQ, something like

C#
var count = XDocument
    .Load("test.xml")
    .XPathSelectElements("//Element")
    .Count();


and to look for particular child see below.


C#
IEnumerable<XElement> elements =
    from el in root.Elements("Element")
    where (string)el.Element("Article") <> ""
    select el;
 
Share this answer
 
v2
Comments
sbarnes 1-Apr-14 1:53am    
good succinct useable answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900