Reputation: 85
I'm trying to format an XML element with three strings padded to 10 characters with spaces. Here is the code I'm using.
The three strings are set in the model.
public string a{ get; set; }
public string b{ get; set; }
public string c{ get; set; }
and then formated to an XElement
var itemElement = new XElement("item",
new XElement("abc", string.Format("{0:-10}{1:-10}{2:-10}", i.a, i.b, i.c))
);
it should be producing "a(padding)b(padding)c(padding)", but is producing "abc" within the node.
Any ideas?
Upvotes: 2
Views: 677
Reputation: 96497
The correct String.Format
syntax is to use a comma for alignment, not a colon:
string.Format("{0,-10}{1,-10}{2,-10}", i.a, i.b, i.c)
The syntax of a format item is as follows:
{index[,length][:formatString]}
Upvotes: 3
Reputation: 164301
Try wrapping in an CData element:
var itemElement = new XElement("item",
new XCData(
new XElement("abc", string.Format("{0:-10}{1:-10}{2:-10}", i.a, i.b, i.c))));
You need this, because whitespace is not considered significant by XML parsers by default. An alternative to using CData would be the xml:space attribute, which should also produce the desired result. See this MSDN page for details.
Upvotes: 0