I need to read an Xml file and I am trying to count the numbers of a spesific tag inside the file. My problem is the same tag represents different things when it is outside some other tag and inside. I am able to get total count of the tag but I need to find the count of the tag that is inside the specified tag.
(I find total cac:AllowanceCharge tags by using XmlNodeList nodeList = xmlDoc.DocumentElement.GetElementsByTagName("cac:AllowanceCharge");)
To Find the "cac:AllowanceCharge" tags that are inside the "cac:InvoiceLine" tags i have tried using:
XmlNodeList elemList = root.GetElementsByTagName(@"//cac:InvoiceLine/cac:AllowanceCharge");
XmlNodeList elemList = root.GetElementsByTagName(@"//InvoiceLine/AllowanceCharge");
XmlNodeList elemList = root.GetElementsByTagName(@"cac:InvoiceLine/cac:AllowanceCharge");
I would appreciate it if someone could tell me the right syntax for it. Thank you.
The first looks correct, if you registered namespace binding cac
//cac:InvoiceLine/cac:AllowanceCharge
There is also another way
//*[name()='InvoiceLine']/*[name()='AllowanceCharge']
To select the nodes by XPath, call the SelectNodes
method:
var root = xmlDoc.DocumentElement;
var nodeList = root.SelectNodes(xpath);
or the overloaded method
var nodeList2 = root.SelectNodes(xpath, namespaceManager);