开发者

How to format the property value based up on the length?

I want to create the file which is in record format.

Example :output file : Record.txt

      SD raj       ,kumar     ,27/04/2011
      DD male  ,single         ,27/04/2011
      ED 27/04/2011

RecordName  Fieldname   FieldLength

SD             FirstName          10
SD           LastName           10
SD          date               15
DD           Sex                 6
DD           maritalstatus      15
DD          date               15
ED          date

The InputFile is Input.txt whose contents are

  raj,kumar,27/04/2011
  male,single,27/04/2011
  27/04/2011

I read this input file and populate the data into the following class object. The comma , is the delimiter used to separate the field values in the record.

class SD
    {

            private string fName;
            private string lName;
            private DateTime date;


            #region Properties
            public string FName
            {
             get { return fName;}
             set { fName= value; }
            }
            public DateTime Date
            {
             get { return date; }
                    set { date = value; }
            }
            public string LName
            {
             get { return lName;}
             set { lName= value; }
            }
            #endregion
    }



    class DD
    {

            private string sex;
         private string mStatus;
         private DateTime date;


         #region Properties
         public string SEX
         {
             get { return sex;}
             set { sex= value; }
            }
            public string Date
            {
                get { return date; }
                set { date = value; }
         }
         public string Mstatus
            {
             get { return mStatus;}
                    set { mStatus= value; }
         }
         #endregion
    }

    class ED
    {

         private DateTime date;


            #region Properties

            public string Date
            {
              get { return date; }
             set { date = value; }
  开发者_StackOverflow中文版          }       
            #endregion
    }

I have also created the xml to get the Field length for each record

Sample.xml

    <Data>
        <SD Fname="10" Lname="10" Date="15/>
        <DD SEX="6" mStatus="15" Date="15/>
        <ED Date="15/>
    <Data/>

Now anyone can tell me the way where can i format the fields?

1.after reading the input text and populate it into the corresponding class object

2.before writing to the outputfile

Tell me the proper solution(must use the sample.xml to identify the field length)


If I understand your question correctly you are wondering how to output the data with fixed field widths? If so you could just create a function to pad the string like this:

    string GetFixedLengthString(string value, int length)
    {
        return (value ?? "").Length > length ? (value ?? "").Substring(0, length) : (value ?? "").PadRight(length);
    }

And call that when you write out the values. I'm guessing the second element in Sample.xml should be ED and not SD? Loading it into an XmlDocument and reading the attributes should be simple enough, then pass the attribute value as the length parameter to the function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜