Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
548 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
ID: 33457499
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
ID: 33457543
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 800 total points
ID: 33457549
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 1200 total points
ID: 33457610
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
ID: 33458027
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Integration Management Part 2
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

885 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