Solved

How to modify a C# console application to hold off on displaying an error record until a field value is read from an input file?

Posted on 2011-09-13
4
262 Views
Last Modified: 2013-12-17
I am developing a C# console application using Visual Studio 2005.

I read as input, an XML file and then I create an Error report, if any of the individual fields within each record have an invalid value.

If a particular field has an error, I display the "Check Number" for this record and the field value in error.

In the code section that follows, I display a sample record that serves as my input file.

The problem I am facing is that I want to display the Check Number field value as the identifying field for each record with an error. However, the Check Number is the 4th field to be read from the XML input file in the field sequence.

Thus, if there is an error in the 1st, 2nd, or 3rd field, I don't have the check number field value yet to display for my error report.

My code is in the code section that follows.

Do you know how the application code could be modified to create error records showing the Check Number value, when an error occurs in any field, including the first 3 fields as follows:

<csc:processing_date>
<csc:item_sequence_number>
<csc:account_number>




Here is the format of the xml file

<csc:item>
<csc:processing_date>20100702</csc:processing_date>
<csc:item_sequence_number>000002500759580</csc:item_sequence_number>
<csc:account_number>00000000000001111111</csc:account_number>
<csc:check_number>000000104216438</csc:check_number>
<csc:amount>0000002206</csc:amount>
<csc:routing_transit>007122258</csc:routing_transit>
<csc:bank_number>0088</csc:bank_number>
<csc:transaction_code></csc:transaction_code>
<csc:data1></csc:data1>
<csc:data2></csc:data2>
<csc:data3></csc:data3>
<csc:userField></csc:userField>
<csc:image_offset>0</csc:image_offset>
<csc:image_length>018218</csc:image_length>
<csc:image_side>A</csc:image_side>
</csc:item>
============================
My C# program is as follows:

using System;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;

namespace ReadXml1
{

    class Class1
    {
        static void Main(string[] args)
        {
           
            string goodFilePath = @"U:\Harris\HarrisCheck.ard.ind";
            string emptyFilePath = @"U:\Harris\HarrisCheck.ard";
            string tiffFilePath = @"U:\Harris\HarrisCheck.ard.out";
            string errorFilePath = @"U:\Harris\HarrisCheck.err.txt";

            if (File.Exists(goodFilePath))
            {
                File.Delete(goodFilePath);
            }

            if (File.Exists(emptyFilePath))
            {
                File.Delete(emptyFilePath);
            }

            if (File.Exists(tiffFilePath))
            {
                File.Delete(tiffFilePath);
            }

            if (File.Exists(errorFilePath))
            {
                File.Delete(errorFilePath);
            }

            
            DirectoryInfo parentDirectory = new DirectoryInfo(@"\\nydfs1\root\APPDATA\CashControl\HARRISBKOnDemandUtility\FTPFILES\Dev");

            
            bool hasElseBeenExecuted = false;  // <-- add this boolean line to use as a check further down

            foreach (FileInfo file in parentDirectory.GetFiles())
            {
                StreamWriter sw = null;
                try
                {
                    if (file != null && XmlFile(file.FullName))

                    //if (XmlFile(file.FullName))
                    {
                        StreamWriter sw1 = new StreamWriter(errorFilePath);
                        sw = new StreamWriter(goodFilePath);
                        ProcessFile(file.FullName, sw, sw1);
                    }
                    else if (!hasElseBeenExecuted)  // <-- change this from a regular "else" to an "else if" that specifically looks to see if the hasElseBeenExecuted variable is set to false
                    {
                        hasElseBeenExecuted = true;  // <-- set the boolean flag to true so that this else block never gets hit again
                        sw = new StreamWriter(emptyFilePath);
                        File.Copy(file.FullName, @"U:\Harris\HarrisCheck.ard.out");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (sw != null) sw.Close();
                }
            }



            if (File.Exists(errorFilePath))
            {
                if (new FileInfo(errorFilePath).Length == 0)
                {
                    if (File.Exists(errorFilePath))
                    {
                        File.Delete(errorFilePath);
                    }
                }

                else
                {
                    if (File.Exists(goodFilePath))

                        File.Delete(goodFilePath);

                    if (File.Exists(emptyFilePath))

                        File.Delete(emptyFilePath);

                    if (File.Exists(tiffFilePath))

                        File.Delete(tiffFilePath);
                }
            }


            Console.WriteLine("Processing done. Press enter to exit ... ");
            Console.ReadLine();
        }

        private static void ProcessFile(string filePath, StreamWriter sw, StreamWriter sw1)
        {
            int i = 0;
            string[,] arr1 = new string[2000, 20];
            int iCheckNumber = 0;
            String formattedString1 = null;
            XmlTextReader reader = null;
            try
            {
                reader = new XmlTextReader(filePath);
                while (reader.Read())
                {

                    if (reader.NodeType == XmlNodeType.Element)
                    {

                        if (reader.Name == "csc:item_sequence_number")
                        {
                            arr1[i, 11] = "GROUP_FIELD_NAME:CpcsNo";
                            String h = reader.ReadElementContentAsString();
                            String formattedString = h.Substring(7, 8);
                            Int64 intTest = 0;

                            if (Int64.TryParse(formattedString, out intTest) && intTest > 0)
                                arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
                            else
                            {
                                arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
                                sw1.WriteLine("GROUP_FIELD_NAME:CheckNumber:" + formattedString1 + ",GROUP_FIELD_NAME:CpcsNo" + "," + "GROUP_FIELD_VALUE:" + formattedString);
                                iCheckNumber--;
                            }
                        }
                        if (reader.Name == "csc:check_number")
                        {
                            iCheckNumber = iCheckNumber + 1;
                            arr1[i, 0] = "COMMENT: CHECK NUMBER #" + iCheckNumber;
                            arr1[i, 1] = "GROUP_FIELD_NAME:CheckNumber";
                            String e = reader.ReadElementContentAsString();
                            String formattedString = e.Substring(6, 3);
                            formattedString1 = e.Substring(9, 6);
                            Int64 intTest = 0;

                            if (Int64.TryParse(formattedString1, out intTest))
                                arr1[i, 2] = "GROUP_FIELD_VALUE:" + formattedString1;
                            else
                            {
                                arr1[i, 2] = "GROUP_FIELD_VALUE:" + formattedString1;
                                sw1.WriteLine("GROUP_FIELD_NAME:CheckNumber:" + formattedString1 + ",GROUP_FIELD_NAME:CheckNumber" + "," + "GROUP_FIELD_VALUE:" + formattedString1);
                            }

                            arr1[i, 15] = "GROUP_FIELD_NAME:OfficeNo";
                            if (Int64.TryParse(formattedString, out intTest) && intTest > 0)
                                arr1[i, 16] = "GROUP_FIELD_VALUE:" + formattedString;
                            else
                            {
                                arr1[i, 16] = "GROUP_FIELD_VALUE:" + formattedString;
                                sw1.WriteLine("GROUP_FIELD_NAME:CheckNumber:" + formattedString1 + ",GROUP_FIELD_NAME:OfficeNo" + "," + "GROUP_FIELD_VALUE:" + formattedString);
                            }
                        }
                        if (reader.Name == "csc:routing_transit")
                        {
                            arr1[i, 3] = "GROUP_FIELD_NAME:RoutingTransit";
                            String g = reader.ReadElementContentAsString();
                            String formattedString = g.Substring(1, 8);
                            Int64 intTest = 0;

                            if (Int64.TryParse(formattedString, out intTest) && intTest > 0)
                                arr1[i, 4] = "GROUP_FIELD_VALUE:" + formattedString;
                            else
                            {
                                arr1[i, 4] = "GROUP_FIELD_VALUE:" + formattedString;
                                sw1.WriteLine("GROUP_FIELD_NAME:CheckNumber:" + formattedString1 + ",GROUP_FIELD_NAME:RoutingTransit" + "," + "GROUP_FIELD_VALUE:" + formattedString);
                            }

                            arr1[i, 5] = "GROUP_FIELD_NAME:BankName";
                            arr1[i, 6] = "GROUP_FIELD_VALUE:HARRIS BANK";
                        }
                        if (reader.Name == "csc:account_number")
                        {
                            arr1[i, 7] = "GROUP_FIELD_NAME:BankAccountNo";
                            String f = reader.ReadElementContentAsString();
                            String formattedString = f.Substring(13, 7);
                            Int64 intTest = 0;

                            if (Int64.TryParse(formattedString, out intTest) && intTest > 0)
                                arr1[i, 8] = "GROUP_FIELD_VALUE:" + formattedString;
                            else
                            {
                                arr1[i, 8] = "GROUP_FIELD_VALUE:" + formattedString;
                                sw1.WriteLine("GROUP_FIELD_NAME:CheckNumber:" + formattedString1 + ",GROUP_FIELD_NAME:BankAccountNo" + "," + "GROUP_FIELD_VALUE:" + formattedString);
                                iCheckNumber--;
                            }

                        }
                        if (reader.Name == "csc:amount")
                        {
                            arr1[i, 9] = "GROUP_FIELD_NAME:CheckAmount";
                            
                            string value = reader.ReadElementContentAsString();
                            Int64 intTest = 0;

                            if (Int64.TryParse(value, out intTest) && intTest > 0)
                                arr1[i, 10] = "GROUP_FIELD_VALUE:" + value.Insert(value.Length - 2, ".");
                            else
                            {
                                arr1[i, 10] = "GROUP_FIELD_VALUE:" + value.Insert(value.Length - 2, ".");
                                sw1.WriteLine("GROUP_FIELD_NAME:CheckNumber:" + formattedString1 + ",GROUP_FIELD_NAME:CheckAmount" + "," + "GROUP_FIELD_VALUE:" + value);
                            }

                            
                        }

                        if (reader.Name == "csc:processing_date")
                        {
                            arr1[i, 13] = "GROUP_FIELD_NAME:CheckPaidDate";
                            String d = reader.ReadElementContentAsString();
                            DateTime parsedDate;
                            IFormatProvider format = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;

                            if (DateTime.TryParseExact(d, "yyyyMMdd", format, DateTimeStyles.None, out parsedDate))
                                arr1[i, 14] = "GROUP_FIELD_VALUE:" + parsedDate.ToShortDateString();
                            else
                            {
                                arr1[i, 14] = "GROUP_FIELD_VALUE:" + parsedDate.ToShortDateString();
                                sw1.WriteLine("GROUP_FIELD_NAME:CheckNumber:" + formattedString1 + ",GROUP_FIELD_NAME:CheckPaidDate" + "," + "GROUP_FIELD_VALUE:" + d);
                                iCheckNumber--;
                            }
                        }

                        if (reader.Name == "csc:image_offset")
                        {
                            string strTemp1 = reader.ReadElementContentAsString();
                            Int64 intTest = 0;

                            if (Int64.TryParse(strTemp1, out intTest))
                                arr1[i, 17] = "GROUP_OFFSET:" + strTemp1.PadLeft(10, '0');
                            else
                            {
                                arr1[i, 17] = "GROUP_OFFSET:" + strTemp1.PadLeft(10, '0');
                                sw1.WriteLine("GROUP_FIELD_NAME:CheckNumber:" + formattedString1 + ",GROUP_OFFSET:" + "," + "GROUP_FIELD_VALUE:" + strTemp1);
                            }
                        }

                        if (reader.Name == "csc:image_length")
                        {
                            string strTemp2 = reader.ReadElementContentAsString();
                            Int64 intTest = 0;

                            if (Int64.TryParse(strTemp2, out intTest) && intTest > 0)
                                arr1[i, 18] = "GROUP_LENGTH:" + strTemp2.PadLeft(10, '0');
                            else
                            {
                                arr1[i, 18] = "GROUP_LENGTH:" + strTemp2.PadLeft(10, '0');
                                sw1.WriteLine("GROUP_FIELD_NAME:CheckNumber:" + formattedString1 + ",GROUP_LENGTH:" + "," + "GROUP_LENGTH:" + strTemp2);
                            }

                            arr1[i, 19] = "GROUP_FILENAME:/clientdoc/HRSBK.HARRISBK.THKHARB4.THKHARB4.77.1.ard.out";
                            i++;
                        }
                    }
                }

                sw.WriteLine("COMMENT: specify code page of the index date");
                sw.WriteLine("CODEPAGE:819");
                for (int iter = 0; iter < i; iter++)
                {
                    for (int j = 0; j < 20; j++)
                    {
                        sw.WriteLine(arr1[iter, j]);
                    }
                }
            }
            finally
            {
                if (sw1 != null) sw1.Close();
                reader.Close();
            }
        }
        static bool XmlFile(string filePath)
        {
            string line;
            bool xmlFile = false;
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(filePath);
                line = sr.ReadLine();
                if (line.Equals("<?xml version=\"1.0\"?>"))
                    xmlFile = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                xmlFile = false;
            }
            finally
            {
                if (sr != null) sr.Close();
            }
            return xmlFile;
        }

    }
}

Open in new window

0
Comment
Question by:zimmer9
  • 2
  • 2
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36530548
Perhaps you could use a Queue to track the preceding fields. When you get to the fourth field, the "Check Number" field, you can dequeue the Queue for that record. Something similar to:

System.Collections.Generic.Queue<string> tempQueue = new System.Collections.Generic.Queue<string>();

// field 1, if in error
tempQueue.Enqueue(field1);

// field 2, if in error
tempQueue.Enqueue(field2);

// field 3, if in error
tempQueue.Enqueue(field3);

// field 4 - "Check Number"
while (s.Count > 0)
{
    string field = s.Dequeue();
    // you should get back field1, field2, field3, in that order, so you can concatenate
    //  or whatever you would like to do with those values
}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36530558
Lines 13 and 15 should read (respectively):

while (tempQueue.Count > 0)

Open in new window


string field = tempQueue.Dequeue();

Open in new window

0
 

Author Comment

by:zimmer9
ID: 36531002
How do I incorporate this Queue into my C# application?

starting out with:

using System;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Collections.Generic;


0
 

Author Closing Comment

by:zimmer9
ID: 36531020
I will subdivide this question into multiple questions.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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