Q:

Complex string parsing for record updation

Consider the below input string.
var inputString = "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp?" + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>" + Environment.NewLine;
            inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;

Let us observe the string content of the "Exceptions" (there are altogether 5 in the example given).
FirstOne
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: 

SecondOne
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

ThirdOne
Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

FourthOne
Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

FifthOne
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

If we look carefully, the First and Second case are identical. So are 3rd and 4th. 5th one is identical to 1st/2nd.
What I need to do is that, if the first and second or the consecutive "Exception" text's are idential, then leaving the 1st one the others will be replace with "Exception: --DO--". 
If an idential string is found but not consecutive, then it will appear as it is.
Henceforth, the output will be (everything of the original string will remain same only the matching exceptions will be updated as shown under)
FirstOne
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

SecondOne (because it is identical to Firstone and is consecutive)
Exception: --DO--

ThirdOne  (because it is a new one)
Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

FourthOne (because it is identical to Thirdone and is consecutive)
Exception: --DO--

FifthOne   (though it is identical to first/second one, but not in a consecutive sequence)
Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

I did this program. First I get the contents of the Exceptions based on the indexes(start and end) and then stored the values inside a collection.Then using loop I was checking the content of the first and teh second and did the needed updation.
But I believe that there are many better solutions than this (may be regular expression, linq, lambda combination.)
Need help to solve this problem in the efficient way.
Thanks

A:

Here is my final version
var inputString = "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp?" + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;

            TextReader reader = new StringReader(inputString);
            string line = null;
            StringBuilder sb = new StringBuilder();
            string firstLine = null;

            //append the first line
            sb.AppendLine(reader.ReadLine());

            //read the first exception line
            firstLine = reader.ReadLine();
            sb.AppendLine(firstLine);

            while ((line = reader.ReadLine()) != null)
            {
                if (line.StartsWith("Exception:"))
                {

                    if (line == firstLine)
                    {
                        sb.AppendLine("Exception:-- Do --");
                    }

                    if (line != firstLine)
                    {
                        sb.AppendLine(line);
                        firstLine = line;

                    }
                }
                else
                {
                    sb.AppendLine(line);
                }
            }

            Console.WriteLine(sb.ToString());

            Console.ReadKey();

