Solved

How to rewrite an IF statement in C# to ensure that an extract file has an output field with a fixed length of 10 and with leading zeros?

Posted on 2010-08-26
3
411 Views
Last Modified: 2013-12-17
I am writing my first C# console application using Visual Studio 2005.
My goal is to have my application read an input file and create an extract file.
Do you know how I could rewrite the following C# code "IF statement" for 1 of my input fields?
2 examples of the input record value are as follows, for the input field titled image_offset:
1)   <csc:image_offset>02894</csc:image_offset>
or
2)   <csc:image_offset>0</csc:image_offset>
 
For the output of this field, the output field is titled "GROUP OFFSET"
The output field should be formatted with leading zeros and with a fixed field length of 10?
The output for the 1st example above should be as follows:
GROUP_OFFSET:0000002894 (6 leading zeros)
The output for the 2nd example should be as follows:
GROUP_OFFSET:0000000000 (10 zeros)

My C# IF statement which needs to be modified is as follows:

if (reader.Name == "csc:image_offset")
                    {
                       arr1[i,17] = "GROUP_OFFSET:" + reader.ReadElementContentAsString();
                    }


Here is an example of an input file for this application:
-------------------------------
<?xml version="1.0"?>
<csc:CIndex_File
    xmlns:csc="http://c.com/xml/CIndex_File">
<csc:header>
    <csc:version>1.0</csc:version>
    <csc:customer_name>              </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: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:transaction_code>
    <csc:data1></csc:data1>
    <csc:data2></csc:data2>
    <csc:data3></csc:data3>
    <csc:userField></csc:userField>
    <csc:image_offset>02894</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:transaction_code>
    <csc:data1></csc:data1>
    <csc:data2></csc:data2>
    <csc:data3></csc:data3>
    <csc:userField></csc:userField>
    <csc:image_offset>0</csc:image_offset>
    <csc:image_length>0000016685</csc:image_length>
    <csc:image_side>A</csc:image_side>
</csc:item>
</csc:CheckVision_Index_File>
-----------------------------------------------------
My program is as follows:

using System;
using System.Xml;
using System.IO;
namespace ReadXml1
{
     class Class1
     {        
         static void Main(string[] args)
         {
            int i = 0;
            string[,] arr1 = new string[1000,19];
            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;
                        arr1[i,0] = "COMMENT: CHECK NUMBER #" + iCheckNumber;
                        arr1[i,1] = "GROUP_FIELD_NAME:CheckNumber";
                        arr1[i,2] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();        
                    }
                    if (reader.Name == "csc:routing_transit")
                    {
                        arr1[i,3] = "GROUP_FIELD_NAME:RoutingTransit";
                        arr1[i,4] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                        arr1[i,5] = "GROUP_FIELD_NAME:BankName";
                        arr1[i,6] = "GROUP_FIELD_VALUE:RBC BANK";
                    }
                    if (reader.Name == "csc:account_number")
                    {
                        arr1[i,7] = "GROUP_FIELD_NAME:BankAccountNo";
                        arr1[i,8] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                    }
                    if (reader.Name == "csc:amount")
                    {
                        arr1[i,9] = "GROUP_FIELD_NAME:CheckAmount";
                        arr1[i,10] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                        arr1[i,11] = "GROUP_FIELD_NAME:CpscNo";
                        arr1[i,12] = "GROUP_FIELD_VALUE:00000000";                        
                    }
                    if (reader.Name == "csc:processing_date")
                    {
                        arr1[i,13] = "GROUP_FIELD_NAME:CheckPaidDate";
                        arr1[i,14] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentAsString();
                        arr1[i,15] = "GROUP_FIELD_NAME:OfficeNo";
                        arr1[i,16] = "GROUP_FIELD_VALUE:000";  
                    }
                    if (reader.Name == "csc:image_offset")
                    {
                        arr1[i,17] = "GROUP_OFFSET:" + reader.ReadElementContentAsString();
                    }
                    if (reader.Name == "csc:image_length")
                    {
                        arr1[i,18] = "GROUP_LENGTH:" + reader.ReadElementContentAsString();
                        i++;
                    }                    
                }            
            }

            for (int iter = 0; iter < i; iter++)
            {
                for (int j = 0; j < 19; j++)
                {
                    sw.WriteLine(arr1[iter,j]);
                }
            }
            sw.Close();
            reader.Close();    
         }                  
     }
}

My output file would look like the following:

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:00000000000000000005
GROUP_FIELD_NAME:CheckAmount
GROUP_FIELD_VALUE:0000001.00
GROUP_FIELD_NAME:CpscNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaidDate
GROUP_FIELD_VALUE:20100805
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:000
GROUP_OFFSET:0000002894
GROUP_LENGTH:0000016685
COMMENT: CHECK NUMBER #2
GROUP_FIELD_NAME:CheckNumber
GROUP_FIELD_VALUE:000000000000002
GROUP_FIELD_NAME:RoutingTransit
GROUP_FIELD_VALUE:000000007
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:RBC BANK
GROUP_FIELD_NAME:BankAccountNo
GROUP_FIELD_VALUE:00000000000000000005
GROUP_FIELD_NAME:CheckAmount
GROUP_FIELD_VALUE:0000002.00
GROUP_FIELD_NAME:CpscNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaidDate
GROUP_FIELD_VALUE:20100805
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:000
GROUP_OFFSET:0000000000
GROUP_LENGTH:0000016685
0
Comment
Question by:zimmer9
  • 2
3 Comments
 
LVL 17

Expert Comment

by:xDJR1875
Comment Utility

Use the PadLeft function of the string, see below.

            string myincomingString = txtIn.Text.Trim();   // 000145   and it needs to be 0000000145
            myincomingString = myincomingString.PadLeft(10, '0');
            MessageBox.Show(myincomingString);



0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
The modified code is here:

            string strTemp = reader.ReadElementContentAsString();

            if (reader.Name == "csc:image_offset")

            {

                arr1[i, 17] = "GROUP_OFFSET:" + strTemp.PadLeft(10,'0');

            }

Open in new window

0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
Comment Utility
Or this may be better:

            if (reader.Name == "csc:image_offset")

            {

                string strTemp = reader.ReadElementContentAsString();

                arr1[i, 17] = "GROUP_OFFSET:" + strTemp.PadLeft(10,'0');

            }

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

13 Experts available now in Live!

Get 1:1 Help Now