• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

Parsing text input files

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.

Thanks!
0
gromul
Asked:
gromul
1 Solution
 
MinutemanCommented:
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();
            tr.Close();

            // 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);
            output.Write(newText);
            output.Close();
        }
    }
}
0
 
gromulAuthor Commented:
Thanks. I ended up splitting a string with " as delimeter.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now