Search And Replace Text in XML File with C#

I would like to search and replace text in an xml file and save a new version of that file.

So, I have an xml file called SS.xml and would like to replace "geltimer1" with "geltimer2" at all levels / nodes and save the file out to SS2.xml.

I have several strings to replace in each file and several files to process, otherwise I would just use Notepad++.

Thanks in Advance
KnightWhoSaysNiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Fernando SotoRetiredCommented:
Please post a sample of the XML file. XML files are case sensitive as well as it has a hierarchy which must be considered.
KnightWhoSaysNiAuthor Commented:
XML file attached.
ss.xml
Fernando SotoRetiredCommented:
Hi KnightWhoSaysNi;

This code snippet will do what you want it to.

using System.Xml.Linq;


// Load the XML document into memory from the file system. Change File Path to yours
XDocument xdoc = XDocument.Load("C:/Working Directory/ss.xml");

// Query the XML document for the string "GELTIMER!" and return it as part of the result set
List<XElement> nodes = (from node in xdoc.Descendants("Property")
                        where node.Attributes("name").Any(n => n.Value != "CONFIGURED_ITEMS") && node.Value.Contains("GELTIMER1")
                        select node).ToList();

// Replace all values of GELTIMER1 with GELTIMER2
nodes.ForEach(n => n.Value = n.Value.Replace("GELTIMER1", "GELTIMER2"));

// Save the xml as SS2.xml
xdoc.Save("C:/Working Directory/SS2.xml");

Open in new window

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
KnightWhoSaysNiAuthor Commented:
Works well.  I just had to setup local variables for the strings.

{
        String InputPath = @"C:\Users\Philip\Desktop\ss.xml";
        String OutputPath = @"C:\Users\Philip\Desktop\Output\ss2.xml";

        // Load the XML document into memory from the file system. Change File Path to yours
        XDocument xdoc = XDocument.Load(InputPath);

        // Query the XML document for the string "GELTIMER!" and return it as part of the result set
        List<XElement> nodes = (from node in xdoc.Descendants("Property")
                                where node.Attributes("name").Any(n => n.Value != "CONFIGURED_ITEMS") && node.Value.Contains("QAL_SS_GELTMR1")
                                select node).ToList();

        // Replace all values of GELTIMER1 with GELTIMER2
        nodes.ForEach(n => n.Value = n.Value.Replace("QAL_SS_GELTMR1", "QAL_SS_GELTMR2"));

        // Save the xml as SS2.xml
        xdoc.Save(OutputPath);
        }

Open in new window

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
C#

From novice to tech pro — start learning today.