c# - Finding count of a keyword in an XML -


xml can have format

<root>     <child1>         value1     </child1>     <child2>             <child3>                 value2             </child3>             <child4>                     <child5>                         value1                     </child5>             </child4>     </child2>     <child6>         value1     </child6> </root> 

here value1 written 3times, count should 3.

or xml can have format this:

<root>     <child1 value="value1" />     <child2 value="value2"/>     <child3 value="value1" />     <child4 value="value1"/>     <child5 value="value3" />     <child6 value="value4"/>     </root> 

here value1 has count 3.

so xml can have format, want count particular keyword count in xml.

please guide !

edit: question not possible duplicate of link have mentioned, here don't know depth of xml e.g. have 1 child or many children in keyword may or may not present. want traverse nodes without know depth of each node**

i suggest using xdocument this.

xdocument doc = xdocument.load(filename);        var count = doc.descendants()                                                     // flattens structure               .where(x=> (!x.haselements &&((string)x.value).trim() == "value1")                             || (x.attribute("value") != null                                  && x.attribute("value").value == "value1")                          )                                                        // filter based on value               .count();                                                           // take count 

check demo


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -