【问题标题】:Import XML and create directories C#导入 XML 并创建目录 C#
【发布时间】:2016-08-04 13:11:01
【问题描述】:

我还处于 XML 文件的起点。我想用 XML 创建目录,我有这个功能:

RunCommand 是一个执行 comanda_cmd 的函数 第一,第二是字符串

private void ProcesNode(XmlNode node, string parentPath, string path, string first, string second, string BuiltUnit, string item)
{

    if (!node.HasChildNodes || ((node.ChildNodes.Count == 2) && (node.FirstChild is System.Xml.XmlText)))
    {
        //MessageBox.Show(parentPath + "/" + node.Name);
    }
    else
    {
        foreach (XmlNode child in node.ChildNodes)
        {
            comanda_cmd = first + "/" + parentPath + second + "/" + parentPath + "/" + node.Name;
            string status = RunCommand(comanda_cmd + "/project.pj /n");
            //content = "_GEN_PROJECT/" + ProjectName + "/" + BuiltUnit + "/" + item + "/" + parentPath + "/" + node.Name + " already exist";
            //MessageBox.Show(content);
            //check_status(status, content);
            ProcesNode(child, parentPath + "/" + node.Name, path, first, second, BuiltUnit, item);
        }
    }
}

我有这个 XML:

<unit>
<Unit1>
    <src>
        <i>
            <test1>
                <test_in1>
                    <test_in_out>
                        <t>
                        </t>
                    </test_in_out>
                </test_in1>
            </test1>
            <test2>
                <test_in2>
                </test_in2>
            </test2>
        </i>
    </src>
    <doc>
        <i>
            <test1>
                <test_in1>
                    <test_in_out>
                        <t>
                        </t>
                    </test_in_out>
                </test_in1>
            </test1>
            <test2>
                <test_in2>
                </test_in2>
            </test2>
        </i>
    </doc>
</Unit1>
<Unit2>
    <src>
        <i>
        </i>
    </src>
</Unit2>

</unit>

我调用 ProcesNode,它会创建目录。例如:unit/Unit1/src/i/test1/test_in1/test_in_out 但没有创建最后一个目录(在我的例子中是“t”)。

我哪里错了?为什么不创建最后一个目录?

【问题讨论】:

  • 对于最低标签 (&lt;t&gt;),if 条件 (!node.HasChildren || ...)true,因此您无需输入创建目录的 else 块。
  • 你是对的!感谢伟大的 Rene Vogt

标签: c# xml path


【解决方案1】:

最低级别的标签(如您的&lt;t&gt; 标签)没有任何子节点。

这就是你的if 声明

if (!node.HasChildNodes || ((node.ChildNodes.Count == 2) && (node.FirstChild is System.Xml.XmlText)))

true(因为!node.HasChildNodestrue). So for the tags on the last level you never enter theelse` 块(可能)创建目录。

看来您可以简单地将那部分 (!node.HasChildNodes ||) 排除在外。如果没有子节点,则不会进入foreach循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    相关资源
    最近更新 更多