xmldocument的顺序错误

本文关键字:错误 顺序 xmldocument | 更新日期: 2023-09-27 18:01:33

我正在构建代码自动化,从读取一个示例选项卡分隔的文档。我取消了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;
        }
    }
}

xmldocument的顺序错误

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

而不是while或将检查器更改为变量到lastsmallest。我赋值列表值而不是索引值