Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string a = String.Format("|{0,5}|{1,5}|{2,5}", 1, 20, 300);
- string b = String.Format("|{0,-5}|{1,-5}|{2,-5}", 1, 20, 300);
- // 'a' will be equal to "| 1| 20| 300|"
- // 'b' will be equal to "|1 |20 |300 |"
- <WriteFixedWidth Table="orders" StartAt="1" Output="Return">
- <Position Start="1" Length="17" Name="Unique Identifier"/>
- <Position Start="18" Length="3" Name="Error Flag"/>
- <Position Start="21" Length="16" Name="Account Number" Justification="right"/>
- <Position Start="37" Length="8" Name="Member Number"/>
- <Position Start="45" Length="4" Name="Product"/>
- <Position Start="49" Length="3" Name="Paytype"/>
- <Position Start="52" Length="9" Name="Transit Routing Number"/>
- </WriteFixedWidth>
- public void WriteFixedWidth(System.Xml.Linq.XElement CommandNode, DataTable Table, Stream outputStream)
- {
- StreamWriter Output = new StreamWriter(outputStream);
- int StartAt = CommandNode.Attribute("StartAt") != null ? int.Parse(CommandNode.Attribute("StartAt").Value) : 0;
- var positions = from c in CommandNode.Descendants(Namespaces.Integration + "Position")
- orderby int.Parse(c.Attribute("Start").Value) ascending
- select new
- {
- Name = c.Attribute("Name").Value,
- Start = int.Parse(c.Attribute("Start").Value) - StartAt,
- Length = int.Parse(c.Attribute("Length").Value),
- Justification = c.Attribute("Justification") != null ? c.Attribute("Justification").Value.ToLower() : "left"
- };
- int lineLength = positions.Last().Start + positions.Last().Length;
- foreach (DataRow row in Table.Rows)
- {
- StringBuilder line = new StringBuilder(lineLength);
- foreach (var p in positions)
- line.Insert(p.Start,
- p.Justification == "left" ? (row.Field<string>(p.Name) ?? "").PadRight(p.Length,' ')
- : (row.Field<string>(p.Name) ?? "").PadLeft(p.Length,' ')
- );
- Output.WriteLine(line.ToString());
- }
- Output.Flush();
- }
- handle.WriteLine(s20.PadRight(20));
- handle.WriteLine(s80.PadRight(80));
- handle.WriteLine(s10.PadRight(10));
- handle.WriteLine(s2.PadRight(2));
Add Comment
Please, Sign In to add comment