【发布时间】:2014-04-03 16:54:24
【问题描述】:
我正在尝试构建一个动态 XDoc,其中包含一个工具用作“路径”的文件夹列表。每个“文件夹”元素都是树中的另一层
例子:
Root Level
-- Folder L0
-- Folder L1
-- Folder L2
在XML中表示如下:
<FolderPath>
<Folder>L0</Folder>
<Folder>L1</Folder>
<Folder>L2</Folder>
</FolderPath>
我的代码如下:
// Build up the innermost folders inside the Folderpath element dynamically
XElement folderPath = new XElement();
folderPath.Add(new XElement(FolderList[0],
new XAttribute("fromDate", FromDate),
//attributes for Folder w/ lots of attributes
new XAttribute("toDate", ToDate),
new XAttribute("contactName", ContactName),
new XAttribute("email", Email),
FolderList[0]));
for (int i = 1; i < FolderList.Count; i++)
{
folderPath.Add(new XElement(FolderList[i]));
}
FolderList 是我在代码中的这一点之前填充的列表。但是我的线路有问题:
XElement folderPath = new XElement();
什么是实现 XElement 以便我可以动态添加 FolderList 中包含的文件夹的好方法?我得到的错误是“System.Xml.Linq.XElement 不包含采用 0 个参数的构造函数”。
【问题讨论】:
-
这已经快一年了,但我在查找一些 XElement 答案时遇到了它,您还需要任何帮助吗?如果您有希望生成的 XML 外观的示例,我可以找出可行的方法。
标签: c# linq-to-xml xelement