[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB Replace XML Node With Text

Posted on 2014-07-20
6
Medium Priority
?
334 Views
Last Modified: 2014-07-20
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.
0
Comment
Question by:rcscs
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 40207185
Why you are doing that  ?
<document>
This is where tag1 was
</document>



Duncan
0
 

Author Comment

by:rcscs
ID: 40207195
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>
0
 
LVL 13

Accepted Solution

by:
duncanb7 earned 2000 total points
ID: 40207199
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
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:rcscs
ID: 40207216
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
0
 

Author Closing Comment

by:rcscs
ID: 40207218
Should have thought of this myself really ! - but happy to award points :)
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40207220
Thanks for your points.

In other words, you can make it with Replace() on VB, Right ?

Duncan
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question