Ask Question Forum:
Model Library:2025-02-08:A.I. model is online auto reply
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
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.
Expert: duncanb7 replied at 2024-07-20 04:44:48
Thanks for your points.
In other words, you can make it with Replace() on VB, Right ?
Duncan
In other words, you can make it with Replace() on VB, Right ?
Duncan
Author: rcscs replied at 2024-07-20 04:40:10
Should have thought of this myself really ! - but happy to award points :)
Author: rcscs replied at 2024-07-20 04:39:00
Thanks Duncan,
This is what I've ended up which seems to do the job.
Dim cTemplate As String = "<template><uppercase>Text To Be Processed</uppercase></template>"
Dim oXML As New XmlDocument
Dim oNodes As XmlNodeList
oXML.LoadXml(cTemplate)
oNodes = oXML.ChildNodes
ProcessNodeTest(cTemplate, oNodes(0))
MsgBox(cTemplate)
...
Private Sub ProcessNodeTest(ByRef cTemplate As String, oNode As XmlNode)
Select Case oNode.Name
Case "uppercase"
cTemplate = Replace(cTemplate, oNode.OuterXml, UCase(oNode.InnerXml))
oNode.ParentNode.RemoveChild(oNode)
End Select
For Each oChildNode In oNode.ChildNodes
ProcessNodeTest(cTemplate, oChildNode)
Next
End Sub
This is what I've ended up which seems to do the job.
Dim cTemplate As String = "<template><uppercase>Text To Be Processed</uppercase></template>"
Dim oXML As New XmlDocument
Dim oNodes As XmlNodeList
oXML.LoadXml(cTemplate)
oNodes = oXML.ChildNodes
ProcessNodeTest(cTemplate, oNodes(0))
MsgBox(cTemplate)
...
Private Sub ProcessNodeTest(ByRef cTemplate As String, oNode As XmlNode)
Select Case oNode.Name
Case "uppercase"
cTemplate = Replace(cTemplate, oNode.OuterXml, UCase(oNode.InnerXml))
oNode.ParentNode.RemoveChild(oNode)
End Select
For Each oChildNode In oNode.ChildNodes
ProcessNodeTest(cTemplate, oChildNode)
Next
End Sub
Accepted Solution
Expert: duncanb7 replied at 2024-07-20 04:16:46
500 points EXCELLENT
I think it is hard besides these method as follows
1- Copy example.xml to exampl.txt and search <tag1 value="testvalue"/> and replace
it with "This is where tag1 was" on VB after open example.txt and then copy back to example.xml
2- Using create CDATA section, look at into MS manual for XmlDocument.CreateCDataSection Method at
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection(v=vs.110).aspx
and after CDATA section and then delete the tag node,<tag1 value="testvalue"/>
Hope understand your question completely.If not, please pt it out
Duncan
1- Copy example.xml to exampl.txt and search <tag1 value="testvalue"/> and replace
it with "This is where tag1 was" on VB after open example.txt and then copy back to example.xml
2- Using create CDATA section, look at into MS manual for XmlDocument.CreateCDataSection Method at
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection(v=vs.110).aspx
and after CDATA section and then delete the tag node,<tag1 value="testvalue"/>
Hope understand your question completely.If not, please pt it out
Duncan
Author: rcscs replied at 2024-07-20 04:12:20
The XML contains tags which need to be processed by my application.
Utimately the XML reduces until there is only a single outer tag and the inner text.
For example:
<document><uppercase>this is the text which will be processed</uppercase></document>
After processing need to be end up as:
<document>THIS IS THE TEXT WHICH NEEDS TO BE PROCESSED</document>
Utimately the XML reduces until there is only a single outer tag and the inner text.
For example:
<document><uppercase>this is the text which will be processed</uppercase></document>
After processing need to be end up as:
<document>THIS IS THE TEXT WHICH NEEDS TO BE PROCESSED</document>
Expert: duncanb7 replied at 2024-07-20 03:45:17
Why you are doing that ?
<document>
This is where tag1 was
</document>
Duncan
<document>
This is where tag1 was
</document>
Duncan