Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help change a particular word in an XML doc

Larry C
Guest
 
Posts: n/a
#1: Nov 12 '05
Hello,

I have an XML doc and I am trying to write a utility thath will allow me to
change a particular word with my "title" section of theXML doc. I would
like to look for a "word" in the title and then change only that "word" to
something else. Below in XML file I would want to change "Issue" to
"Concern". This is one snip of the XML file there may be many more
instances of the word "Issue" that I would want to change. I know how to
walk through the file and find the whole instance of a string, ie Add/Raise
Issue and then replace that entire string but I can't figure out how to find
and replace just a word in a string. I have tried some VB6 stuff but I am
hoping that there is the magic function in .Net

FYI - I am not that great of a programmer.
Thanks
Larry C
- <screen>
<screen-name>Raise_Issue</screen-name>
<parameter key="title" value="Add / Raise Issue" direct="true" />
<parameter key="banner" value="/jsps/common/jsps/EmagBanner.jsp" />
<parameter key="Role" value="/jsps/common/jsps/Role.jsp" />
<parameter key="body" value="/jsps/idm/Issues_AddRaiseApprove.jsp" />
<parameter key="footer" value="/jsps/common/jsps/footer.jsp" />
</screen>


Derek Harmon
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Need help change a particular word in an XML doc


"Larry C" <lccroninMAPSON@hotmail.com> wrote in message news:uXrMUtStEHA.3320@TK2MSFTNGP15.phx.gbl...[color=blue]
> I have an XML doc and I am trying to write a utility thath will allow me
> to change a particular word with my "title" section of theXML doc.[/color]
: :[color=blue]
> Below in XML file I would want to change "Issue" to "Concern".[/color]

The "magic" solution would be to use the Replace( ) method of
String, like this:

Dim xmlDoc As XmlDocument = New XmlDocument( )
xmlDoc.Load( "screen.xml")

' Replace all occurrences of Issue with Concern and
' re-parse the XML into xmlDoc.
'
xmlDoc.LoadXml( xmlDoc.OuterXml.Replace( "Issue", "Concern") )

Console.WriteLine( xmlDoc.OuterXml)

Having said that, while that one-liner may be acceptable when
prototyping or working with small XML documents, it's inefficient
for larger XML documents and doesn't give fine control over
which instances of "Issue" you want to replace (for example,
perhaps you only want to replace Issue when it appears as
an attribute value, the text node of an element, or when it
appears in specific contexts).

A better approach in these circumstances would be to subclass
(extend) XmlTextReader and override it's subs and properties
to check strings as they are read (perhaps when only certain
conditions are true, to narrow the context in which replacements
occur) and replace selectively. The reason for this performing
better is that you're only calling Replace on smaller strings (not
the whole document) and it doesn't require re-parsing the
document (because you can give the XmlTextReader subclass
to XmlDocument's Load( ) method and the content is parsed
one time only).


Derek Harmon


L C
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Need help change a particular word in an XML doc


Thanks Derek,

That's how I am doing it now. I was hoping for the one line solution.

Larry
"Derek Harmon" <loresayer@msn.com> wrote in message
news:uuPIN5YtEHA.2624@TK2MSFTNGP11.phx.gbl...[color=blue]
> "Larry C" <lccroninMAPSON@hotmail.com> wrote in message[/color]
news:uXrMUtStEHA.3320@TK2MSFTNGP15.phx.gbl...[color=blue][color=green]
> > I have an XML doc and I am trying to write a utility thath will allow me
> > to change a particular word with my "title" section of theXML doc.[/color]
> : :[color=green]
> > Below in XML file I would want to change "Issue" to "Concern".[/color]
>
> The "magic" solution would be to use the Replace( ) method of
> String, like this:
>
> Dim xmlDoc As XmlDocument = New XmlDocument( )
> xmlDoc.Load( "screen.xml")
>
> ' Replace all occurrences of Issue with Concern and
> ' re-parse the XML into xmlDoc.
> '
> xmlDoc.LoadXml( xmlDoc.OuterXml.Replace( "Issue", "Concern") )
>
> Console.WriteLine( xmlDoc.OuterXml)
>
> Having said that, while that one-liner may be acceptable when
> prototyping or working with small XML documents, it's inefficient
> for larger XML documents and doesn't give fine control over
> which instances of "Issue" you want to replace (for example,
> perhaps you only want to replace Issue when it appears as
> an attribute value, the text node of an element, or when it
> appears in specific contexts).
>
> A better approach in these circumstances would be to subclass
> (extend) XmlTextReader and override it's subs and properties
> to check strings as they are read (perhaps when only certain
> conditions are true, to narrow the context in which replacements
> occur) and replace selectively. The reason for this performing
> better is that you're only calling Replace on smaller strings (not
> the whole document) and it doesn't require re-parsing the
> document (because you can give the XmlTextReader subclass
> to XmlDocument's Load( ) method and the content is parsed
> one time only).
>
>
> Derek Harmon
>
>[/color]


Closed Thread


Similar .NET Framework bytes