Question How Do I Record an Attribute's Path/Location?

Doug

Active member
Joined
Oct 5, 2011
Messages
44
Programming Experience
Beginner
I am trying to update attribute values between two XML documents. The attributes are in the same node location in the two files but the value needs to be changed. I cannot search by attribute name because more than one node has the same name. This will be saved in an XML delta file.

I am considering recreating all the nodes going back to the root while deleting all the data except for the node path and the actual attribute that I need to change.

Is there an easier way to do this?
 
Here is the function that I created that strips down all of the nodes going back to the root. I then save the "path" node. When I open the file I intend to change the value I iterate back up the path in the target xml file until I find that value and change it.

This just feels like I'm doing this the hard way but I can't find any .net methods to do the same thing.

VB.NET:
 Private Function createPathNode(ByVal attribute As XmlAttribute) As XmlNode
    'This function creates a XML node that holds the node path from root to the attribute
    Dim nodeHold As XmlNode = attribute.OwnerElement
    nodeHold.RemoveAll()
    nodeHold.Attributes.Append(attribute)
    Dim parentNode As XmlNode = nodeHold.ParentNode
    Do Until parentNode Is Nothing
      parentNode.RemoveAll()
      parentNode.AppendChild(nodeHold)
      nodeHold = parentNode
      parentNode = nodeHold.ParentNode
    Loop
    Return nodeHold
  End Function
 
I knew I was doing it the hard way. Xpath was the answer to my original question. Now I just need to learn how to use it and generate it.
 
Back
Top