xml counts

  • Thread starter Thread starter DaveL
  • Start date Start date
D

DaveL

Is there a method/funciton in xmldocument
to count all nodes in a xml doc

or do i have to write my own

thanks
DAveL
 
Is there a method/funciton in xmldocument
to count all nodes in a xml doc

or do i have to write my own

thanks
DAveL

On XmlDocument there is a SelectNodes() method that accepts an XPath
expression and returns a list of matching nodes. Get the Count of that.

Hans Kesting
 
DaveL said:
Is there a method/funciton in xmldocument
to count all nodes in a xml doc

or do i have to write my own

It depends on the kind of nodes you are looking for
xmlDocumentInstance.SelectNodes("//*").Count
gives you the number of all element nodes
xmlDocumentInstance.SelectNodes("//@*").Count
the number of all attribute nodes (in the XPath 1.0 data model where
namespace declarations are not attributes).
 
It happens that DaveL formulated :
Right, but it only has the count of its nodes
child node mite have child node example

<ROOT>
<ONE>
<TWO/>
<THREE/>
<TWO/>

</ONE>
<TWO>
<THREE/>
</TWO>
</ONE>
</ROOT>

In the above i have a total of 6 nodes, i wonderd if XmlDocument Has a Count
For all Nodes in a complete Document
Seems Like i'll need to Write a method/function for recursion through the
document

Dave L

The snippet below produces a result of 7 (I changed the XML somewhat to
make it well-formed)

public static void RunSnippet()
{
string s = @"<ROOT>
<ONE>
<TWO>
<THREE/>
</TWO>

</ONE>
<ONE>
<TWO>
<THREE/>
</TWO>
</ONE>
</ROOT>";

XmlDocument xml = new XmlDocument();
xml.LoadXml(s);

XmlNodeList nl = xml.SelectNodes("//*");
Console.WriteLine(nl.Count);

}



Hans Kesting
 
Thanks alot hans
Much Appriciated
DaveL

Hans Kesting said:
It happens that DaveL formulated :

The snippet below produces a result of 7 (I changed the XML somewhat to
make it well-formed)

public static void RunSnippet()
{
string s = @"<ROOT>
<ONE>
<TWO>
<THREE/>
</TWO>

</ONE>
<ONE>
<TWO>
<THREE/>
</TWO>
</ONE>
</ROOT>";

XmlDocument xml = new XmlDocument();
xml.LoadXml(s);

XmlNodeList nl = xml.SelectNodes("//*");
Console.WriteLine(nl.Count);

}



Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top