C# XDocument XElement构造函数

C# XDocument XElement构造函数,c#,linq-to-xml,xelement,C#,Linq To Xml,Xelement,我试图构建一个动态XDoc,其中包含一个工具用作“路径”的文件夹列表。每个“文件夹”元素都是树中的另一层 例如: Root Level -- Folder L0 -- Folder L1 -- Folder L2 以XML表示,如下所示: <FolderPath> <Folder>L0</Folder> <Folder>L1</Folder> <Folder>L2&l

我试图构建一个动态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]));
        }

实现XElement以便动态添加FolderList中包含的文件夹的好方法是什么?我得到的错误是“System.Xml.Linq.XElement不包含接受0个参数的构造函数”。

在XElement类中有
无参数的构造函数,例如,您应该这样初始化它

XElement xFolderPath = new XElement("FolderPath"); 
它接受字符串,因为它可以隐式地转换为
XName


另一个解决问题的棘手方法是定义
xFolderPath
对象实例
XElement
没有无参数构造函数。您想要使用的构造函数需要一个XName来分配给XElement,并且还可以选择传递该XElement的内容

您可以在下面创建
XElement
folderPath变量的代码中看到,我使用
XElement(XName name,params object[]content)
,在这里传递
XElement
的名称,在本例中,我将
XAttribute
对象数组作为其内容传递

之后,我创建了一个名为previousNode的临时
XElement
对象,并将folderPath对象分配给它

在for循环中,我使用
XElement(XName)
构造函数创建一个名为newNode的新
XElement
,并将其作为内容添加到previousNode
XElement
,然后将previousNode设置为新创建的新节点,因此任何附加元素都将作为该
XElement
的子元素添加,创建我假设您想要的层次结构,如下面的代码示例所示

using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace CommandLineProgram
{
    public class DefaultProgram
    {
        public static void Main(string[] args)
        {
            List<String> FolderList = new List<string>() { "L0", "L1", "L2" };
            DateTime FromDate = DateTime.Now;
            DateTime ToDate = DateTime.Now;
            String ContactName = "ContactName";
            String Email = "contact@email.com";

            XElement folderPath = 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));

            XElement previousNode = folderPath;
            for (int i = 1; i < FolderList.Count; i++)
            {
                XElement newNode = new XElement(FolderList[i]);
                previousNode.Add(newNode);
                previousNode = newNode;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Xml.Linq;
名称空间命令行程序
{
公开课计划
{
公共静态void Main(字符串[]args)
{
List FolderList=新列表(){“L0”、“L1”、“L2”};
DateTime FromDate=DateTime.Now;
DateTime ToDate=DateTime.Now;
字符串ContactName=“ContactName”;
字符串电子邮件=”contact@email.com";
XElement folderPath=新XElement(FolderList[0],
新XAttribute(“fromDate”,fromDate),
//包含大量属性的文件夹的属性
新XAttribute(“toDate”,toDate),
新XAttribute(“contactName”,contactName),
新的XAttribute(“email”,email));
XElement previousNode=文件夹路径;
for(int i=1;i
ToString()输出


Nice one@JulieShannon。这已经快一岁了,但我在查找一些XElement答案时发现了它,您还需要任何帮助吗?如果您有一个示例,说明您希望生成的XML是什么样子的,那么我可以找出一些可行的方法。
using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace CommandLineProgram
{
    public class DefaultProgram
    {
        public static void Main(string[] args)
        {
            List<String> FolderList = new List<string>() { "L0", "L1", "L2" };
            DateTime FromDate = DateTime.Now;
            DateTime ToDate = DateTime.Now;
            String ContactName = "ContactName";
            String Email = "contact@email.com";

            XElement folderPath = 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));

            XElement previousNode = folderPath;
            for (int i = 1; i < FolderList.Count; i++)
            {
                XElement newNode = new XElement(FolderList[i]);
                previousNode.Add(newNode);
                previousNode = newNode;
            }
        }
    }
}