XElement 中的 string.Format 未格式化 [英] string.Format in XElement not formatting

查看:34
本文介绍了XElement 中的 string.Format 未格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用三个用空格填充到 10 个字符的字符串来格式化一个 XML 元素.这是我正在使用的代码.

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; }

然后格式化为 XElement

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))
                    );

它应该产生a(padding)b(padding)c(padding)",但在节点内产生abc".

it should be producing "a(padding)b(padding)c(padding)", but is producing "abc" within the node.

有什么想法吗?

推荐答案

正确的 String.Format 语法是使用逗号而不是冒号进行对齐:

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)

每个 MSDN:

格式项的语法如下如下:

The syntax of a format item is as follows:

{index[,length][:formatString]}

{index[,length][:formatString]}

这篇关于XElement 中的 string.Format 未格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆