xmldocument的顺序错误

时间:2013-02-28 19:14:42

标签: c# automation code-generation

我正在通过阅读示例制表符分隔文档来构建代码自动化。我的订单是关闭xml文档结构。我的逻辑出了什么问题?我看到两种情况来找到xml节点的正确父节点。

for (int lines = 0; lines < files.Length; lines++)  //start
{
    var firstNode = files[lines].TrimEnd('\t');
    NodeNames.Add(files[lines].Replace("\t","") + lines);

    levels.Add(TextTool.CountStringOccurrences(firstNode, "\t"));  //first count
    if (lines == 0)
    {
        newXml += "XmlNode " + NodeNames[lines] + "Node" + " = xmlDoc.CreateElement(\"" + files[lines].Replace("\t", "") + "\");" + System.Environment.NewLine;
        //set value to empty
        newXml += NodeNames[lines] + "Node" + ".InnerText = String.Empty;" + System.Environment.NewLine;
        newXml += "xmlDoc.AppendChild(" + NodeNames[lines] + "Node);" + System.Environment.NewLine;

    }
    else{
        if (levels[levels.Count - 1] > levels[levels.Count - 2])
        {
            //create new ele
            newXml += "XmlNode " + NodeNames[lines] + "Node" + " = xmlDoc.CreateElement(\"" + files[lines].Replace("\t", "") + "\");" + System.Environment.NewLine;
            //set value to empty
            newXml += NodeNames[lines] + "Node" + ".InnerText = String.Empty;" + System.Environment.NewLine;
            //add new ele as child to previous ele
            newXml += NodeNames[lines - 1] + "Node" + ".AppendChild(" + NodeNames[lines] + "Node" + ");" + System.Environment.NewLine;
        }
        else
        {
            //create new ele
            newXml += "XmlNode " + NodeNames[lines] + "Node" + " = xmlDoc.CreateElement(\"" + files[lines].Replace("\t", "") + "\");" + System.Environment.NewLine;
            var lastSmallest = levels.Count - 1;
            var current = levels[levels.Count - 1];
            var checker = levels[levels.Count - 1];
            while (checker >= current)
            {
                lastSmallest = lastSmallest - 1;
                checker = levels[lastSmallest];
            }
            //set node value to empty;
            newXml += NodeNames[lines] + "Node" + ".InnerText = String.Empty;" + System.Environment.NewLine;
            newXml += NodeNames[checker] + "Node" + ".AppendChild(" + NodeNames[lines] + "Node" + ");" + System.Environment.NewLine;

        }
    }
}

1 个答案:

答案 0 :(得分:0)

                    for (int i = lines; i > 0;i-- )
                    {
                        if (levels[i] < levels[lastSmallest])
                        {
                            checker = i;
                            break;
                        }
                    }

而不是while或将checker更改为lastsmallest。我正在分配列表值而不是索引值