Replace XML Element Content using VB


I am new to XML and have a couple of questions. I have a XML document. How can I replace the content of the elements using VB? I want to replace the content with another similar XML document. Some of the information will be there, and some not. I hope this is enough information to get started.

Here is a small sample of my XML:

<publisher>Who Published</publisher>
<rights>Who has rights<rights>

Open in new window

Who is Participating?
William ElliottConnect With a Mentor Sr Tech GuruCommented:
here is one i use.
you can either script it or at commandline you can just type
"cscript replace.vbs C:\Scripts\file.xml Bob superman"
and it will replace all instances of bob with superman

Const ForReading = 1
Const ForWriting = 2
If Wscript.Arguments.Count <> 3 Then
WScript.Echo "example" & VbCrLf & "cscript replace.vbs C:\Scripts\Text.txt Jim James"
End if
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.WriteLine strNewText

Open in new window

ALawrence007Author Commented:
Thanks weellio,

Would this work if I want to replace just the title maybe or maybe the title and the publisher?

<publisher>Who Published</publisher>
William ElliottSr Tech GuruCommented:
to make sure i understand correctly,.
do you just want to replace one word with another word?
if so, then the script above will do exactly what you need

start - run
type "replace.vbs C:\Scripts\file.xml Doctitle newdoctitle"
and all instances will be replaced.

or you can just open the file in notepad and click edit-replace

if you want a script to seach and find <title>sometitle</title> then replace everying between the 'title' fields, then i'll need to write a script that is a little different.
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.

All Courses

From novice to tech pro — start learning today.