我可以使用的XDocument建立以下文件,该文件工作正常:如何使用foreach和LINQ构建XDocument?
XDocument xdoc = new XDocument
(
new XDeclaration("1.0", "utf-8", null),
new XElement(_pluralCamelNotation,
new XElement(_singularCamelNotation,
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
),
new XElement(_singularCamelNotation,
new XElement("id", "2"),
new XElement("whenCreated", "2008-12-31")
)
)
);
然而,我需要建立由XML文件通过收集迭代这样的:
XDocument xdoc = new XDocument
(
new XDeclaration("1.0", "utf-8", null));
foreach (DataType dataType in _dataTypes)
{
XElement xelement = new XElement(_pluralCamelNotation,
new XElement(_singularCamelNotation,
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
));
xdoc.AddInterally(xelement); //PSEUDO-CODE
}
There is Add,AddFirst,AddAfter自我,AddBeforeSelf,但我不能让他们在这方面工作。
像这样可以迭代LINQ吗?
答:
我把吉米的代码暗示本根标签,改变它一点,它正是我一直在寻找:
var xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement(_pluralCamelNotation,
_dataTypes.Select(datatype => new XElement(_singularCamelNotation,
new XElement("id", "1"),
new XElement("whenCreated", "2008-12-31")
))
)
);
马克Gravell发布更好的回答这on this StackOverflow question。
很光滑,谢谢! – 2009-06-16 16:08:36