XML Node count
-
Hello all I need help to count node and their respective id. 1 2 how can i count person node..?? and Person Id = 001 & 002 Thanx in advance.. Shazz here to help you...
-
Hello all I need help to count node and their respective id. 1 2 how can i count person node..?? and Person Id = 001 & 002 Thanx in advance.. Shazz here to help you...
Assuming that People is the root of the document, you can iterate the nodes like this. - Assumes the ID's are integers, you can change it to String if you want. - written without syntax checking. Sorry if its a bit off, but you can get the idea.
Imports System.Xml
Private Sub CountNodes(xmlDoc as XmlDocument)
Dim xNode as XmlNode
Dim nodeName as String
Dim nodeCount as Integer
Dim listOfIDs as New List(Of Integer)For Each xNode in xmlDoc.DocumentElement.ChildNodes
If String.IsNullOrEmpty(xNode.Name) = False Then
nodeName = xNode.Name.Trim().ToLower()
If nodeName = "person" Then
nodeCount++
If node.Attributes("ID") IsNot Nothing Then
listOfIDs.Add(Convert.ToInt32(node.GetAttributeValue("ID")))
End If 'node attr
End If 'node name
End If 'node has name
Next 'xNodeEnd Sub
You can also use Xpath Query to do the same, or if the nodes can be nested you will need a recursive function.
-
Assuming that People is the root of the document, you can iterate the nodes like this. - Assumes the ID's are integers, you can change it to String if you want. - written without syntax checking. Sorry if its a bit off, but you can get the idea.
Imports System.Xml
Private Sub CountNodes(xmlDoc as XmlDocument)
Dim xNode as XmlNode
Dim nodeName as String
Dim nodeCount as Integer
Dim listOfIDs as New List(Of Integer)For Each xNode in xmlDoc.DocumentElement.ChildNodes
If String.IsNullOrEmpty(xNode.Name) = False Then
nodeName = xNode.Name.Trim().ToLower()
If nodeName = "person" Then
nodeCount++
If node.Attributes("ID") IsNot Nothing Then
listOfIDs.Add(Convert.ToInt32(node.GetAttributeValue("ID")))
End If 'node attr
End If 'node name
End If 'node has name
Next 'xNodeEnd Sub
You can also use Xpath Query to do the same, or if the nodes can be nested you will need a recursive function.
Thanx for your refernce code. Partially its working fine though I am using VB6.0 application and your mention code belong to vb.net. I am slight confuse for declaration of `Node` because non of IXMLDom related datatype have properties of GetAtttributeValues("ID"). Please guide me how can I proceed. I had search and i tried many things help. Refernece as I had used "Microsoft XML v6.0" Thanx in advance. Awaiting for your reply.
Shazz here to help you...
-
Thanx for your refernce code. Partially its working fine though I am using VB6.0 application and your mention code belong to vb.net. I am slight confuse for declaration of `Node` because non of IXMLDom related datatype have properties of GetAtttributeValues("ID"). Please guide me how can I proceed. I had search and i tried many things help. Refernece as I had used "Microsoft XML v6.0" Thanx in advance. Awaiting for your reply.
Shazz here to help you...
Sorry, I no longer have VB6 available to help you with this exactly. The GetAttributeValues() method will get the value of an XMLNode (element)'s attribute. I cant find any IXMLDom documentation to look for a similar function, but if you can post a link I can try to find it.