【问题标题】:XDocument XElement ConstructorXDocument XElement 构造函数
【发布时间】: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


【解决方案1】:

XElement 类中有no parameter-less constructor,你应该像这样初始化它,例如

XElement xFolderPath = new XElement("FolderPath"); 

它接受字符串,因为它可以将implicitly 转换为XName

另一个解决问题的棘手方法是定义 xFolderPath 对象实例

【讨论】:

  • 不错的@JulieShannon。
【解决方案2】:

XElement 没有无参数构造函数。您要使用的构造函数需要将 XName 分配给 XElement,并且您也可以选择传递 XElement 的内容。

您可以在下面的代码中看到创建 XElement 文件夹路径变量的位置,我使用的是 XElement(XName name, params object[] content),您在其中传递了 XElement 的名称,在这种情况下,我传递了一个XAttribute 对象的数组作为它的内容。

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

在 for 循环中,我使用 XElement(XName name) 构造函数创建了一个名为 newNode 的新 XElement,并将其作为内容添加到 previousNode XElement,然后将 previousNode 设置为新创建的 newNode,因此任何其他元素将作为 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;
            }
        }
    }
}

folderPath.ToString() 输出

<L0 fromDate="2015-03-23T16:13:52.6702528-05:00" 
    toDate="2015-03-23T16:13:52.6702528-05:00" 
    contactName="ContactName" 
    email="contact@email.com">
  <L1>
    <L2 />
  </L1>
</L0>

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2011-04-22
    相关资源
    最近更新 更多