Parsing text input files

Posted on 2006-05-23
Last Modified: 2010-08-05
I have some input files that I need to update in certain places.

The old format is something like this:
<start param1="..." param2="" param3="" param4="false" />

The new format should look something like this:
<start param1="..." param2="" newParam="true" param3="" param4="false" />

Some sample code to accomplish this would be helpful.

Question by:gromul
    LVL 1

    Accepted Solution

    using System.IO;

    namespace MyNamespace
        public class MyClass

            /// <summary>
            /// Insert newParam="true" into file.
            /// </summary>
            /// <param name="path">The complete file path to be read.</param>
            public void UpdateFile(string path)
                // Read the file into a string
                TextReader tr = new StreamReader(path);
                string fileText = tr.ReadToEnd();

                // Replace all occurrences of oldValue
                string oldValue = "param2=\"\" param3=\"\"";
                string newValue = "param2=\"\" newParam=\"true\" param3=\"\"";
                string newText = fileText.Replace(oldValue, newValue);

                // Overwrite the file with newText
                TextWriter output = File.CreateText(path);

    Author Comment

    Thanks. I ended up splitting a string with " as delimeter.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now