Solved

How to assign a dynamic value to array elements within a C# console application using Visual Studio 2005?

Posted on 2010-08-18
1
473 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 because the order of the output records is different than the order of the input records. To get around this problem, I plan on populating the output records into an array with fixed indexes to get the output records in their proper sequence.

Do you know how I could convert the following C# code IF statement snippet from my C# program to assign the value to an array element?

string[] ar = new string[19];

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 my C# code be convered to assign their values to array elements?
The second element, ar(1), has a static value but the first and third elements of the array contain variable data and I don't know how to assign their values to ar(0) and ar(2).

arr1[0] = "COMMENT: CHECK NUMBER #" + iCheckNumber;
arr1[1] = "GROUP_FIELD_NAME:CheckNumber";
arr1[2] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
*********************************************************************************************************

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
1 Comment
 
LVL 3

Accepted Solution

by:
with earned 500 total points
ID: 33466592
Hi zimmer9,

Try this,
string[] ar = new string[19]; 

if (reader.Name == "csc:check_number")
{
    iCheckNumber++; //increments iCheckNumber number by 1!
    ar[0] = "COMMENT: CHECK NUMBER # " + iCheckNumber.ToString();
    ar[1] = "GROUP_FIELD_NAME:CheckNumber";
    ar[2] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
}

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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…

747 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

15 Experts available now in Live!

Get 1:1 Help Now