简体   繁体   中英

XDocument XElement Constructor

I'm trying to build out a dynamic XDoc which contains a list of Folders that a tool uses as a "path". Each "Folder" element is another layer in the tree

Example:

Root Level

 -- Folder L0

      -- Folder L1

         -- Folder L2

Which is expressed in the XML as follows:

<FolderPath>
   <Folder>L0</Folder>
   <Folder>L1</Folder>
   <Folder>L2</Folder>
</FolderPath>

My code is as follows:

        // 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 is a List that I populate prior to this point in the code. However I'm having issues with the line:

XElement folderPath = new XElement();

What is a good way to implement the XElement so that I can dynamically add the folders contained in FolderList? The error I'm getting is "System.Xml.Linq.XElement does not contain a constructor that takes 0 arguments".

There is no parameter-less constructor in XElement class you should initialize it like this for example

XElement xFolderPath = new XElement("FolderPath"); 

it accepts string as it can be implicitly converted to XName

another tricky way to overcome your issue is to define xFolderPath object instance

XElement does not have a parameter-less constructor. The constructor you would want to use requires an XName to assign to the XElement, and optionally, you can pass the content of that XElement as well.

You can see in the code below where the XElement folderPath variable is created, I'm using the XElement(XName name, params object[] content) , where you pass the name of the XElement , and in this case, I'm passing an array of XAttribute objects as it's content.

After that, I created a temporary XElement object called previousNode, and assigned the folderPath object to it.

In the for loop, I'm creating a new XElement called newNode with the XElement(XName name) constructor, and adding it as content to the previousNode XElement , then setting the previousNode as the newly created newNode, so any additional elements will be added as children of that XElement , creating the hierarchy that I'm assuming you wanted, which is shown below the code sample.

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() output

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM