Solved

How to rewrite a C# console application so that the records are written out in their proper order?

Posted on 2010-08-17
5
519 Views
Last Modified: 2013-12-17
I am writing my first C# application. It's a console application using Visual Studio 2005.

My input file is named C:\\rbc.xml
My output file is name C:\\output.txt

I wrote the following C# code but my output records are not written out in the proper sequence.
Do you know how I could modify the following C# code to get the output records to be written in their proper order?

C# code:

using System;
using System.Xml;
using System.IO;
namespace ReadXml1
{
     class Class1
     {
         
         static void Main(string[] args)
         {
            int iCheckNumber = 0;  
            XmlTextReader reader = new XmlTextReader("C:\\rbc.xml");
           

            StreamWriter sw = new StreamWriter("C:\\output.txt");

            while (reader.Read())
             
            {

                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "csc:check_number")
                    {
                        iCheckNumber = iCheckNumber + 1;
                        sw.WriteLine("COMMENT: CHECK NUMBER # {0}", iCheckNumber);
                        sw.WriteLine("GROUP_FIELD_NAME:CheckNumber");
                        sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
                    }
                    if (reader.Name == "csc:routing_transit")
                    {
                        sw.WriteLine("GROUP_FIELD_NAME:RoutingTransit");
                        sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
                        sw.WriteLine("GROUP_FIELD_NAME:BankName");
                        sw.WriteLine("GROUP_FIELD_VALUE:HARRIS BANK");
                    }
                    if (reader.Name == "csc:account_number")
                    {
                        sw.WriteLine("GROUP_FIELD_NAME:BankAccountNo");
                        sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());                        
                    }
                    if (reader.Name == "csc:amount")
                    {
                        sw.WriteLine("GROUP_FIELD_NAME:CheckAmount");
                        sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
                        sw.WriteLine("GROUP_FIELD_NAME:CpscNo");
                        sw.WriteLine("GROUP_FIELD_VALUE:00000000");
                    }
                    if (reader.Name == "csc:processing_date")
                    {
                        sw.WriteLine("GROUP_FIELD_NAME:CheckPaidDate");
                        sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
                        sw.WriteLine("GROUP_FIELD_NAME:OfficeNo");
                        sw.WriteLine("GROUP_FIELD_VALUE:000");
                    }
                    if (reader.Name == "csc:image_offset")
                    {
                        sw.WriteLine("GROUP_OFFSET:" + reader.ReadElementContentAsString());                  
                    }
                    if (reader.Name == "csc:image_length")
                    {
                        sw.WriteLine("GROUP_LENGTH:" + reader.ReadElementContentAsString());
                    }
                }
            }
            sw.Close();
            reader.Close();
         }
     }
}

My input file is named C:\\rbc.xml and it's contents are as follows:
-----------------------------------------
 <?xml version="1.0" ?>
- <csc:CheckVision_Index_File xmlns:csc="https://c.com/xml/CheckVision_Index_File">
- <csc:header>
  <csc:version>1.0</csc:version>
  <csc:customer_name />
  <csc:request_id>MD</csc:request_id>
  <csc:creation_date>20100805</csc:creation_date>
  <csc:creation_time>084027</csc:creation_time>
  <csc:creation_host>hbd-chvcore</csc:creation_host>
  <csc:content_type>CHECK</csc:content_type>
  <csc:item_count>2</csc:item_count>
  <csc:image_file_name>J.img</csc:image_file_name>
  <csc:input_request_file />
  </csc:header>
- <csc:item>
  <csc:processing_date>20100805</csc:processing_date>
  <csc:item_sequence_number>000000000000001</csc:item_sequence_number>
  <csc:account_number>00000000000000000005</csc:account_number>
  <csc:check_number>000000000000001</csc:check_number>
  <csc:amount>0000000100</csc:amount>
  <csc:routing_transit>000000007</csc:routing_transit>
  <csc:bank_number>0002</csc:bank_number>
  <csc:transaction_code />
  <csc:data1 />
  <csc:data2 />
  <csc:data3 />
  <csc:userField />
  <csc:image_offset>0000000000</csc:image_offset>
  <csc:image_length>0000016685</csc:image_length>
  <csc:image_side>A</csc:image_side>
  </csc:item>
- <csc:item>
  <csc:processing_date>20100805</csc:processing_date>
  <csc:item_sequence_number>000000000000002</csc:item_sequence_number>
  <csc:account_number>00000000000000000005</csc:account_number>
  <csc:check_number>000000000000002</csc:check_number>
  <csc:amount>0000000200</csc:amount>
  <csc:routing_transit>000000007</csc:routing_transit>
  <csc:bank_number>0002</csc:bank_number>
  <csc:transaction_code />
  <csc:data1 />
  <csc:data2 />
  <csc:data3 />
  <csc:userField />
  <csc:image_offset>0000000000</csc:image_offset>
  <csc:image_length>0000016685</csc:image_length>
  <csc:image_side>A</csc:image_side>
  </csc:item>
  </csc:CheckVision_Index_File>

My output file is name C:\\output.txt SHOULD BE AS FOLLOWS:

COMMENT: CHECK NUMBER # 1

GROUP_FIELD_NAME:CheckNumber

GROUP_FIELD_VALUE: 000000000000001

GROUP_FIELD_NAME:RoutingTransit

GROUP_FIELD_VALUE: 000000007

GROUP_FIELD_NAME:BankName

GROUP_FIELD_VALUE:RBC BANK

GROUP_FIELD_NAME:BankAccountNo

GROUP_FIELD_VALUE: 00000000000000000001

GROUP_FIELD_NAME:CheckAmount

GROUP_FIELD_VALUE: 0000001523

GROUP_FIELD_NAME:CpcsNo

GROUP_FIELD_VALUE:00000000

GROUP_FIELD_NAME:CheckPaidDate

GROUP_FIELD_VALUE:08/05/10

GROUP_FIELD_NAME:OfficeNo

GROUP_FIELD_VALUE:000

GROUP_OFFSET:0000000000

GROUP_LENGTH:0000016685

COMMENT: CHECK NUMBER # 2

GROUP_FIELD_NAME:CheckNumber

GROUP_FIELD_VALUE: 000000904216438

GROUP_FIELD_NAME:RoutingTransit

GROUP_FIELD_VALUE: 000000007

GROUP_FIELD_NAME:BankName

GROUP_FIELD_VALUE:RBC BANK

GROUP_FIELD_NAME:BankAccountNo

GROUP_FIELD_VALUE: 00000000000000000001

GROUP_FIELD_NAME:CheckAmount

GROUP_FIELD_VALUE: 00000022.06

GROUP_FIELD_NAME:CpcsNo

GROUP_FIELD_VALUE:00000000

GROUP_FIELD_NAME:CheckPaidDate

GROUP_FIELD_VALUE:08/05/10

GROUP_FIELD_NAME:OfficeNo

GROUP_FIELD_VALUE:000

GROUP_OFFSET:0000000000

GROUP_LENGTH:0000016685

My output records currently appear in the following sequence WHICH IS WRONG:

GROUP_FIELD_NAME:CheckPaidDate
GROUP_FIELD_VALUE:20100805
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:000
GROUP_FIELD_NAME:BankAccountNo
GROUP_FIELD_VALUE:00000000000000000001
COMMENT: CHECK NUMBER # 1
GROUP_FIELD_NAME:CheckNumber
GROUP_FIELD_VALUE:000000000000001
GROUP_FIELD_NAME:CheckAmount
GROUP_FIELD_VALUE:0000000100
GROUP_FIELD_NAME:CpscNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:RoutingTransit
GROUP_FIELD_VALUE:000000007
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:RBC BANK
GROUP_OFFSET:0000000000
GROUP_LENGTH:0000016685
GROUP_FIELD_NAME:CheckPaidDate
GROUP_FIELD_VALUE:20100805
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:000
GROUP_FIELD_NAME:BankAccountNo
GROUP_FIELD_VALUE:00000000000000000001
COMMENT: CHECK NUMBER # 2
GROUP_FIELD_NAME:CheckNumber
GROUP_FIELD_VALUE:000000000000002
GROUP_FIELD_NAME:CheckAmount
GROUP_FIELD_VALUE:0000000200
GROUP_FIELD_NAME:CpscNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:RoutingTransit
GROUP_FIELD_VALUE:000000007
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:RBC BANK
GROUP_OFFSET:0000000000
GROUP_LENGTH:0000016685

0
Comment
Question by:zimmer9
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:WesWilson
Comment Utility
Your fields are appearing in the text file in the same order they appear in the XML file. The reader just reads sequentially through the XML file, so when it hits your If conditions, the first one that evaluates to True is the processing date, not the check number.
0
 

Author Comment

by:zimmer9
Comment Utility
How true.

Do you know how I can rewrite the C# code to write out the records in their proper sequence?
0
 
LVL 8

Assisted Solution

by:WesWilson
WesWilson earned 200 total points
Comment Utility
I would suggest loading the file into an XmlDocument, then iterating through the XmlNodes returned by a call to SelectNodes("//csc:item"). For each node, query the children by xpath, so that you read them in the desired order instead of the order they appear in the file.
0
 
LVL 3

Accepted Solution

by:
Kraeven earned 300 total points
Comment Utility
This won't work, the reader reads the file if something found it will be written at once.
Depending on the chronology of the xml.

You could add the values to an array, but give it fixed indexes.
So the array can be looped through reading each value one by one and writing it in the correct sequence to your file.

Example:
dim ar() as string

ar(0) ="COMMENT: CHECK NUMBER # 1"
ar(1)="GROUP_FIELD_NAME:CheckNumber"
ar(2)="GROUP_FIELD_VALUE: 000000000000001"
0
 

Author Comment

by:zimmer9
Comment Utility
If I use the approach of working with an array, then my first 3 records are attained as follows from my code:

if (reader.Name == "csc:check_number")
                    {
                        iCheckNumber = iCheckNumber + 1;
                        sw.WriteLine("COMMENT: CHECK NUMBER # {0}", iCheckNumber);
                        sw.WriteLine("GROUP_FIELD_NAME:CheckNumber");
                        sw.WriteLine("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
                    }

How could the above lines of C# code be convered to assign their values to an array?
I realize the second element is HARD CODED but the first and third elements of the array contain variable data.

ar(0) = ???      ("COMMENT: CHECK NUMBER # {0}", iCheckNumber);

ar(1)="GROUP_FIELD_NAME:CheckNumber"

ar(2)= ???       ("GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString());
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 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

10 Experts available now in Live!

Get 1:1 Help Now