0

我得到一组具有默认值的“属性”,我想创建一个字符串,它是如下的部分 xml 文档(不是完整的 xml 文档)。我想在每个属性之间都有一个“标签”。在这种情况下,我发现字符串连接是可行的选择。在.Net中有没有更好的方法来做到这一点?

<XmlNodes>
<ChildNode Attribute1 ="100"    Attribute2="200"    Attribute3 ="0"/>
<ChildNode Attribute1="100"     Attribute2="200"    Attribute3 ="0"/>
...
</XmlNodes>
4

1 回答 1

1

Here's a fun little hack that at least only resorts to string manipulation late in the piece:

var nodes = new XElement("XmlNodes");
foreach (var i in Enumerable.Range(1,10))
{
    nodes.Add(new XElement("ChildNode", 
        new XAttribute("Attribute1", 100), 
        new XAttribute("Attribute2", 200), 
        new XAttribute("Attribute3", 0)));
}

var result = nodes.ToString().Replace("\" A", "\"\tA"); // **" A** becomes **"\tA**

But I maintain that if this is about providing 'templates' for people to edit then you're best off getting them to edit it in a non-XML format (like CSV) and then convert to XML when you parse it.

于 2012-04-30T08:12:06.340 回答