| 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] |