我编写了一个c#代码,它显示存储在数组中的值。
static void Main()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\\shreyas\\NX_Temp\\NX_Temp\\000048_A\\CompareReport3D.xml");
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("*");
List<XmlNode> returnedmatchNode=TraverseXml(nodeList);
for (int iIndex = 0; iIndex < returnedmatchNode.Count; iIndex++)
{
Console.WriteLine("values" +returnedmatchNode[iIndex].Value);
}
}
public static List<XmlNode> TraverseXml(XmlNodeList nodeList, int counter = 1)
{
List<XmlNode> matchNode = new List<XmlNode>();
foreach (XmlNode node1 in nodeList)
{
if (node1.Attributes != null && node1.Attributes["match"] != null)
{
matchNode.Add(node1.Attributes["match"]);
}
TraverseXml(node1.ChildNodes, counter);
}
Console.ReadLine();
return matchNode;
}xml代码:
<?xml version="1.0" encoding="utf-8"?>
<xd:xmldiff version="1.0" srcDocHash="11928043053884448382" options="IgnoreChildOrder IgnoreNamespaces IgnoreWhitespace IgnoreXmlDecl " fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff">
<xd:node match="2">
<xd:node match="2">
<xd:node match="19">
<xd:node match="2">
<xd:add>Y</xd:add>
</xd:node>
</xd:node>
<xd:add match="/2/2/11" opid="2" />
<xd:change match="18" name="OWNINGSITE">
<xd:node match="2">
<xd:remove match="1" />
</xd:node>
</xd:change>
<xd:add match="/2/2/2-9" opid="1" />
<xd:change match="17" name="STATUS">
<xd:node match="2">
<xd:remove match="1" />
</xd:node>
</xd:change>
<xd:remove match="14-16" />
<xd:remove match="13" subtree="no">
<xd:remove match="1-2" />
</xd:remove>
<xd:remove match="11" opid="2" />
<xd:remove match="10" />
<xd:remove match="2-9" opid="1" />
<xd:remove match="1" />
</xd:node>
<xd:node match="5">
<xd:node match="3">
<xd:node match="11">
<xd:change match="1">0,1,0,1,0,0,0,0,1</xd:change>
</xd:node>
</xd:node>
</xd:node>
</xd:node>
<xd:descriptor opid="1" type="move" />
<xd:descriptor opid="2" type="move" />
</xd:xmldiff>我正在尝试显示returnedmatchNode的值。但它没有显示任何价值。我无法找出代码中的错误。
发布于 2016-01-13 16:25:30
将递归调用更改为
matchNode.AddRange(TraverseXml(node1.ChildNodes, counter));否则,您将不包括递归的结果。
而且,为了任何运行您的代码的人的理智,请将ReadLine()移动到Main的末尾
发布于 2016-01-13 16:24:49
您总是实例化一个新的List<XmlNode>而不向它添加新的结果:
// this gets called in each round of your recursion
List<XmlNode> matchNode = new List<XmlNode>();在您的方法中,您只是将直接的子节点添加到该列表中。您需要“携带”您的中间结果到下一次递归。
只需替换
TraverseXml(node1.ChildNodes, counter);使用
matchNode.AddRange(TraverseXml(node1.ChildNodes, counter));https://stackoverflow.com/questions/34771159
复制相似问题