Microsoft
Software
Hardware
Network
Question : How to format a date field in the format MM/DD/YY for a C# console application?
I am writing my first C# console application using Visual Studio 2005. Do you know how I could rewrite the following C# code "IF statement" so that the CheckPaidDate output field is formatted as MM/DD/YY?
An example of one of my daily input file records for the date field is as follows:
<csc:processing_date>20100
817</csc:p
rocessing_
date>
The output should be in format MM/DD/YY, for example:
Output fields: GROUP_FIELD_NAME:CheckPaid
Date
GROUP_FIELD_VALUE:08/17/10
To handle this date field, I wrote the following C# IF statement:
if (reader.Name == "csc:processing_date")
{
arr1[i,13] = "GROUP_FIELD_NAME:CheckPai
dDate";
arr1[i,14] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentA
sString();
}
--------------------------
-----
<?xml version="1.0"?>
<csc:CIndex_File
xmlns:csc="
http://c.com/xm
l/CIndex_F
ile
">
<csc:header>
<csc:version>1.0</csc:vers
ion>
<csc:customer_name> </csc:customer_name>
<csc:request_id>MD </csc:request_id>
<csc:creation_date>2010080
5</csc:cre
ation_date
>
<csc:creation_time>084027<
/csc:creat
ion_time>
<csc:creation_host>hbd-chv
core</csc:
creation_h
ost>
<csc:content_type>CHECK</c
sc:content
_type>
<csc:item_count>2</csc:ite
m_count>
<csc:image_file_name>J.img
</csc:imag
e_file_nam
e>
<csc:input_request_file></
csc:input_
request_fi
le>
</csc:header>
<csc:item>
<csc:processing_date>20100
805</csc:p
rocessing_
date>
<csc:item_sequence_number>
0000000000
00001</csc
:item_sequ
ence_numbe
r>
<csc:account_number>000000
0000000000
0005 </csc:account_number>
<csc:check_number>00000000
0000001</c
sc:check_n
umber>
<csc:amount>0000000100</cs
c:amount>
<csc:routing_transit>00000
0007</csc:
routing_tr
ansit>
<csc:bank_number>0002</csc
:bank_numb
er>
<csc:transaction_code></cs
c:transact
ion_code>
<csc:data1></csc:data1>
<csc:data2></csc:data2>
<csc:data3></csc:data3>
<csc:userField></csc:userF
ield>
<csc:image_offset>00000000
00</csc:im
age_offset
>
<csc:image_length>00000166
85</csc:im
age_length
>
<csc:image_side>A</csc:ima
ge_side>
</csc:item>
<csc:item>
<csc:processing_date>20100
805</csc:p
rocessing_
date>
<csc:item_sequence_number>
0000000000
00002</csc
:item_sequ
ence_numbe
r>
<csc:account_number>000000
0000000000
0005</csc:
account_nu
mber>
<csc:check_number>00000000
0000002</c
sc:check_n
umber>
<csc:amount>0000000200</cs
c:amount>
<csc:routing_transit>00000
0007</csc:
routing_tr
ansit>
<csc:bank_number>0002</csc
:bank_numb
er>
<csc:transaction_code></cs
c:transact
ion_code>
<csc:data1></csc:data1>
<csc:data2></csc:data2>
<csc:data3></csc:data3>
<csc:userField></csc:userF
ield>
<csc:image_offset>00000000
00</csc:im
age_offset
>
<csc:image_length>00000166
85</csc:im
age_length
>
<csc:image_side>A</csc:ima
ge_side>
</csc:item>
</csc:CheckVision_Index_Fi
le>
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.t
xt");
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_FIEL
D_NAME:Che
ckNumber")
;
//sw.WriteLine("GROUP_FIEL
D_VALUE:" + reader.ReadElementContentA
sString())
;
arr1[i,0] = "COMMENT: CHECK NUMBER #" + iCheckNumber;
arr1[i,1] = "GROUP_FIELD_NAME:CheckNum
ber";
arr1[i,2] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentA
sString();
}
if (reader.Name == "csc:routing_transit")
{
arr1[i,3] = "GROUP_FIELD_NAME:RoutingT
ransit";
arr1[i,4] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentA
sString();
arr1[i,5] = "GROUP_FIELD_NAME:BankName
";
arr1[i,6] = "GROUP_FIELD_VALUE:RBC BANK";
//sw.WriteLine("GROUP_FIEL
D_NAME:Rou
tingTransi
t");
//sw.WriteLine("GROUP_FIEL
D_VALUE:" + reader.ReadElementContentA
sString())
;
//sw.WriteLine("GROUP_FIEL
D_NAME:Ban
kName");
//sw.WriteLine("GROUP_FIEL
D_VALUE:RB
C BANK");
}
if (reader.Name == "csc:account_number")
{
//sw.WriteLine("GROUP_FIEL
D_NAME:Ban
kAccountNo
");
//sw.WriteLine("GROUP_FIEL
D_VALUE:" + reader.ReadElementContentA
sString())
;
arr1[i,7] = "GROUP_FIELD_NAME:BankAcco
untNo";
arr1[i,8] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentA
sString();
}
if (reader.Name == "csc:amount")
{
//sw.WriteLine("GROUP_FIEL
D_NAME:Che
ckAmount")
;
//sw.WriteLine("GROUP_FIEL
D_VALUE:" + reader.ReadElementContentA
sString())
;
//sw.WriteLine("GROUP_FIEL
D_NAME:Cps
cNo");
//sw.WriteLine("GROUP_FIEL
D_VALUE:00
000000");
arr1[i,9] = "GROUP_FIELD_NAME:CheckAmo
unt";
arr1[i,10] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentA
sString();
arr1[i,11] = "GROUP_FIELD_NAME:CpscNo";
arr1[i,12] = "GROUP_FIELD_VALUE:0000000
0";
}
if (reader.Name == "csc:processing_date")
{
//sw.WriteLine("GROUP_FIEL
D_NAME:Che
ckPaidDate
");
//sw.WriteLine("GROUP_FIEL
D_VALUE:" + reader.ReadElementContentA
sString())
;
//sw.WriteLine("GROUP_FIEL
D_NAME:Off
iceNo");
//sw.WriteLine("GROUP_FIEL
D_VALUE:00
0");
arr1[i,13] = "GROUP_FIELD_NAME:CheckPai
dDate";
arr1[i,14] = "GROUP_FIELD_VALUE:" + reader.ReadElementContentA
sString();
arr1[i,15] = "GROUP_FIELD_NAME:OfficeNo
";
arr1[i,16] = "GROUP_FIELD_VALUE:000";
}
if (reader.Name == "csc:image_offset")
{
//sw.WriteLine("GROUP_OFFS
ET:" + reader.ReadElementContentA
sString())
;
arr1[i,17] = "GROUP_OFFSET:" + reader.ReadElementContentA
sString();
}
if (reader.Name == "csc:image_length")
{
//sw.WriteLine("GROUP_LENG
TH:" + reader.ReadElementContentA
sString())
;
arr1[i,18] = "GROUP_LENGTH:" + reader.ReadElementContentA
sString();
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:CheckNumb
er
GROUP_FIELD_VALUE:00000000
0000001
GROUP_FIELD_NAME:RoutingTr
ansit
GROUP_FIELD_VALUE:00000000
7
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:RBC BANK
GROUP_FIELD_NAME:BankAccou
ntNo
GROUP_FIELD_VALUE:00000000
0000000000
05
GROUP_FIELD_NAME:CheckAmou
nt
GROUP_FIELD_VALUE:0000001.
00
GROUP_FIELD_NAME:CpscNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaid
Date
GROUP_FIELD_VALUE:20100805
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:000
GROUP_OFFSET:0000000000
GROUP_LENGTH:0000016685
COMMENT: CHECK NUMBER #2
GROUP_FIELD_NAME:CheckNumb
er
GROUP_FIELD_VALUE:00000000
0000002
GROUP_FIELD_NAME:RoutingTr
ansit
GROUP_FIELD_VALUE:00000000
7
GROUP_FIELD_NAME:BankName
GROUP_FIELD_VALUE:RBC BANK
GROUP_FIELD_NAME:BankAccou
ntNo
GROUP_FIELD_VALUE:00000000
0000000000
05
GROUP_FIELD_NAME:CheckAmou
nt
GROUP_FIELD_VALUE:0000002.
00
GROUP_FIELD_NAME:CpscNo
GROUP_FIELD_VALUE:00000000
GROUP_FIELD_NAME:CheckPaid
Date
GROUP_FIELD_VALUE:20100805
GROUP_FIELD_NAME:OfficeNo
GROUP_FIELD_VALUE:000
GROUP_OFFSET:0000000000
GROUP_LENGTH:0000016685
Answer : How to format a date field in the format MM/DD/YY for a C# console application?
Ooops, here it is in MM/DD/YY format:
String d = reader.ReadElementContentA
sString();
String formattedDate = d.Substring(4, 2) + "/" + d.Substring(6, 2) + "/" + d.Substring(2, 2);
arr1[i,14] = "GROUP_FIELD_VALUE:" + formattedDate;
Random Solutions
All Exchange 2007 email shows internal IP address of ISA server rather than origin address
Java Not Working in Internet Explorer or Firefox
How can I databind with a default entry of string.Empty
"404 - File not found" for Default .aspx on IIS 7.5
How to save images and image file paths into SQL from delphi form
Comparison Netgear ReadyNAS vs. Buffalo Terastation
System.InvalidCastExceptio
n was unhandled by user code Error code on LINQ
OWA setup issues in Exchange 2010
Trying to upload files to my site
OneNote 2010 default search using last scope