Asked by rcscs
at 2024-07-20 03:03:40
Point:500 Replies:6 POST_ID:829032USER_ID:11935
Topic:
.NET;;
Hi
I'm trying to replace a node in a XML string with text.
i.e.
<document>
<tag1 value="testvalue"/>
</document>
I want to replace the entrie 'tag1' node with a string so I get (for example):
<document>
This is where tag1 was
</document>
I can identify the tag1 node and I can remove it (using .RemoveChild) but I can't find a way to replace it with text value.
So I want to replace the node with a string but at the same position in the xml 'string' as the original tag.
Note: I don't want to replace the innerxml/text of the node I want to replace the entire node with text only.
Test Code I have so far - which will remove the node but not replace it with anything.
Dim oXML As New XmlDocument
Dim oNodes As XmlNodeList
oXML.LoadXml("<document><tag1>ORIGINAL TAG ONE TEXT</tag1></document>")
oNodes = oXML.ChildNodes
ProcessNodeTest(cTemplate, oNodes(0))
...
Sub ProcessNodeTest(oNode As XmlNode)
Select Case oNode.Name
Case "tag1"
oNode.ParentNode.RemoveChild(oNode)
End Select
For Each oChildNode In oNode.ChildNodes
ProcessNodeTest(oChildNode)
Next
End Sub
Assistance gratefully received.
I'm trying to replace a node in a XML string with text.
i.e.
<document>
<tag1 value="testvalue"/>
</document>
I want to replace the entrie 'tag1' node with a string so I get (for example):
<document>
This is where tag1 was
</document>
I can identify the tag1 node and I can remove it (using .RemoveChild) but I can't find a way to replace it with text value.
So I want to replace the node with a string but at the same position in the xml 'string' as the original tag.
Note: I don't want to replace the innerxml/text of the node I want to replace the entire node with text only.
Test Code I have so far - which will remove the node but not replace it with anything.
Dim oXML As New XmlDocument
Dim oNodes As XmlNodeList
oXML.LoadXml("<document><tag1>ORIGINAL TAG ONE TEXT</tag1></document>")
oNodes = oXML.ChildNodes
ProcessNodeTest(cTemplate, oNodes(0))
...
Sub ProcessNodeTest(oNode As XmlNode)
Select Case oNode.Name
Case "tag1"
oNode.ParentNode.RemoveChild(oNode)
End Select
For Each oChildNode In oNode.ChildNodes
ProcessNodeTest(oChildNode)
Next
End Sub
Assistance gratefully received.