I am using StringBuilder to append the elements to a string. The elements must be stored in "fixed"
positions in the string so that when it is loaded into the flat file, elements appear in the correct poistion.
Here is a sample file layout
Element Start Position Length
First Name 0 15
Last Name 15 20
Address 35 25
Example:
I read an element
I append the name "John" into the string starting at position 0.
When I then read the element
I must also append the "whitespace" so that the string "Doe" is in position 15 of the string.
Below is a sample of the code I am using:
nodeLst = doc.GetElementsByTagName("firstname");
b.Append(nodeLst.Item(0).InnerText); //inner text would be John
while (c < 15)
{
b.Append(" "); //adds the whitespace so the next element is in the right position
c++;
In the code, b is the name of my string, nodeLst is an XmlNodeList and c is an integer used
as a counter.
This works but it is obviously tedious as this process must be repeated for every element that
must be appended to the string.
Is there a simpler solution I am overlooking? I am somewhat of a neophyte C# programmer.
Any help would be appreciated.
vinczhangPosted Aug 24, 2005, 12:11 PM
b.Append(nodeLst.Item(0).InnerText); //inner text would be John
while (c < 15)
{
b.Append(" "); //adds the whitespace so the next element is in the right position
c++;
you can try this:
nodeLst.Item(0).InnerText.ToString().PadRight(15,' ');
it will pad spaces to the right for total 15 char long