Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从“;字符串/that/are/path”;在C中#_C#_Xml_String_Path_Tree - Fatal编程技术网

C# 从“;字符串/that/are/path”;在C中#

C# 从“;字符串/that/are/path”;在C中#,c#,xml,string,path,tree,C#,Xml,String,Path,Tree,参考上面链接中给出的问题,我正在寻找类似的C#实现 有人能帮我找到代码吗。下面是我的方法。我当然非常感谢其他人的反馈 var paths = new[] { "nodeA1", "nodeA1/nodeB1/nodeC1", "nodeA1/nodeB1/nodeC1/nodeD1/

参考上面链接中给出的问题,我正在寻找类似的C#实现


有人能帮我找到代码吗。

下面是我的方法。我当然非常感谢其他人的反馈

var paths = new[]
                        {
                            "nodeA1",
                            "nodeA1/nodeB1/nodeC1",
                            "nodeA1/nodeB1/nodeC1/nodeD1/nodeE1",
                            "nodeA1/nodeB1/nodeC2",
                            "nodeA1/nodeB2/nodeC2"
                        };

var xml = new XElement("xml");

foreach (var path in paths)
{
    var parts = path.Split('/');
    var current = xml;
    foreach (var part in parts)
    {
        if (current.Element(part) == null)
        {
            current.Add(new XElement(part));
        }
        current = current.Element(part);
    }
}

var result = xml.ToString();
这将打印以下内容:

<xml>
  <nodeA1>
    <nodeB1>
      <nodeC1>
        <nodeD1>
          <nodeE1 />
        </nodeD1>
      </nodeC1>
      <nodeC2 />
    </nodeB1>
    <nodeB2>
      <nodeC2 />
    </nodeB2>
  </nodeA1>
</xml>


您为什么不自己尝试一下,如果您有特殊问题,请回来?谢谢。在visual studio中运行代码时出现错误。此外,还应进行哪些更改以包括重复路径。比如我有两条路径,比如“nodeA1/nodeB1/nodeC2”,“nodeA1/nodeB1/nodeC2”。如果你能帮我,我将不胜感激。