我可以使用的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