0
錯誤的訂單我正在通過閱讀示例選項卡分隔文檔來構建代碼自動化。我的訂單是關閉的XML文檔結構。我的邏輯有什麼問題?我看到兩種情況可以找到xml節點的正確父節點。xmldocument
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;
}
}
}
請給出XML,預期輸出和實際輸出。 – 2013-02-28 19:16:35
好奇你爲什麼要編寫代碼來生成代碼來解析你的輸入,當你可以直接解析輸入? – 2013-02-28 19:24:27
我的節點相等的情況下是破碎的看着它 – 2013-02-28 21:58:20