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
539 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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 300 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

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.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

634 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