Alejandro A Alejandro A - 2 years ago 104 Question

Read different xml Nodes with same names

for the following xml


I'm trying to run a code to go throught all the objects Student.

What I did so far is (taking into account xmldoc is my xmlDocuemtn variable)

Dim AllStudentNodes = xmldoc.Selectnodes("/Student")

For each item in AllStudentNodes

Name = xmlDoc.SelectSingleNode("/Student/Name").InnerText


What happens it is that it will only go take the first student name twice if there are two nodes named Student, three if there are three Nodes named Student..

It seems like it will get stuck on the first node as many times and Student nodes there are, even if those are different, it won't iterate through all of them.

Any suggestions?


xmlCXMLNode = xmlDoc.SelectSingleNode("cXML")

Dim ItemOutNodes = xmlCXMLNode.SelectNodes("/cXML/Request/OrderRequest/ItemOut")

For Each Item In ItemOutNodes

Node_IO_LineNumber = Item.SelectSingleNode("/cXML/Request/OrderRequest/ItemOut").Attributes("lineNumber")
IO_LineNumber = GetInnerText(Node_IO_LineNumber)
Node_IO_Quantity = Item.SelectSingleNode("/cXML/Request/OrderRequest/ItemOut").Attributes("quantity")
IO_Quantity = GetInnerText(Node_IO_Quantity)
Node_IO_RequestedDeliverYDate = Item.SelectSingleNode("/cXML/Request/OrderRequest/ItemOut").Attributes("requestedDeliveryDate")
IO_RequestedDeliveryDate = GetInnerText(Node_IO_RequestedDeliverYDate).Split("+")
IO_RequestedDeliveryDateInsert = IO_RequestedDeliveryDate(0).Replace("T", " ")
Node_IO_IID_SupplierPartID = Item.SelectSingleNode("/cXML/Request/OrderRequest/ItemOut/ItemID/SupplierPartID")
IO_IID_SupplierPartID = GetInnerText(Node_IO_IID_SupplierPartID)
IO_IID_SupplierPartID = IO_IID_SupplierPartID.Replace("#", "Ñ")
Node_IO_IID_BuyerPartID = Item.SelectSingleNode("/cXML/Request/OrderRequest/ItemOut/ItemID/BuyerPartID")
IO_IID_BuyerPartID = GetInnerText(Node_IO_IID_BuyerPartID)
Node_IO_ID_UP_Money = Item.SelectSingleNode("/cXML/Request/OrderRequest/ItemOut/ItemDetail/UnitPrice/Money")
IO_ID_UP_Money = GetInnerText(Node_IO_ID_UP_Money)
Node_IO_ID_Descrption = Item.SelectSingleNode("/cXML/Request/OrderRequest/ItemOut/ItemDetail/Description")
IO_ID_Description = GetInnerText(Node_IO_ID_Descrption)
Catch ex As Exception
Response.Write("Error ItemOut")
ErrorFound = True
ErrorString = ErrorString + "-Error ItemOut"
End Try

Answer Source

It happens because you actually request the first node matching the XPath in the whole document.
You need to find your Name element within the currently iterated item.

First thing which is need to mentionted is that your XML is not valid.
XML must have single root element.

Let's assume you have the following document:


Then, you can do it this way:

Dim AllStudentNodes = xmldoc.SelectNodes("root/Student")

For each item in AllStudentNodes

Name = item.SelectSingleNode("Name").InnerText


Note that it is item.SelectSingleNode, not xmlDoc.SelectSingleNode.
I.e., "find the single name in my item", not "find the single (first) name in my whole document".

Here is the working .NET Fiddle Demo.


In your code you use the following XPath:


which is absolute XPath and actually means "from the root find the first element matching cXML/Request...". So, slash in the beginning makes it search from root document as well.
Your XPath should be relative. Since your Item is already ItemOut, it can be simply:


It is as simple as folder structure or URL.
You open URL and if you meet a relative link like ItemDetail.html, then it will open If you place a slash in the beginning like /ItemDetail.html, it will open absolute

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download