select nodes count

  • Thread starter Thread starter David
  • Start date Start date
D

David

This is my xml sample:

<po>
<PurchaseOrderDetail>
<ProductLineItem>
<LineNumber>1</LineNumber>
<ProductIdentification>
<aaa>...</aaa>
<bbb />
</ProductIdentification>
<cc />
</ProductLineItem>
....
</PurchaseOrderDetail>
<PurchaseOrderDetail>
<ProductLineItem>
<LineNumber>2</LineNumber>
<ProductIdentification>
<aaa>...</aaa>
<bbb />
</ProductIdentification>
<cc />
</ProductLineItem>
....
</PurchaseOrderDetail>
</po>

I want to select "PurchaseOrderDetail" tag count, like this sample, I
should get "2". How should I get the count of it?

Thanks, David.
 
I use selectnodes:

XmlElement root = doc.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("/PO/PurchaseOrderDetail");
int li_TotalCount = nodeList.Count;

But the li_TotalCount always "0"
I want get "2"...

Thanks, David.
 
Hi,
You can do it like this.

XmlNodeList nodeList = doc.GetElementsByTagName("PurchaseOrderDetail");
int li_TotalCount = nodeList.Count;

I think this will give you a count of 2.

Regards,
Angrez
 
David said:
I use selectnodes:

XmlElement root = doc.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("/PO/PurchaseOrderDetail");
int li_TotalCount = nodeList.Count;

But the li_TotalCount always "0"
I want get "2"...

Thanks, David.

Here you use "PO" in capitals, your example uses "po" in lowercase.
Is that a typo in your post? If not, then there is your problem: XML is case-sensitive
and yours has only "/po/PurchaseOrderDetail" nodes and no "/PO/PurchaseOrderDetail"

Hans Kesting
 
Thank you, Hans.
I solved it already.

David

Hans said:
Here you use "PO" in capitals, your example uses "po" in lowercase.
Is that a typo in your post? If not, then there is your problem: XML is case-sensitive
and yours has only "/po/PurchaseOrderDetail" nodes and no "/PO/PurchaseOrderDetail"

Hans Kesting
 
Back
Top