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
483 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
[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
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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