VB Replace XML Node With Text

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.
rcscsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

duncanb7Commented:
Why you are doing that  ?
<document>
This is where tag1 was
</document>



Duncan
0
rcscsAuthor Commented:
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
duncanb7Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

rcscsAuthor Commented:
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
rcscsAuthor Commented:
Should have thought of this myself really ! - but happy to award points :)
0
duncanb7Commented:
Thanks for your points.

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

Duncan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.