C# 显示arraylist的值时没有输出

C# 显示arraylist的值时没有输出,c#,.net,arraylist,C#,.net,Arraylist,我编写了一个c代码,它显示存储在arraylist中的值 static void Main() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"C:\\shreyas\\NX_Temp\\NX_Temp\\000048_A\\CompareReport3D.xml"); XmlNodeList nodeList = xmlDoc.Docum

我编写了一个c代码,它显示存储在arraylist中的值

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;
        }
static void Main()
{
XmlDocument xmlDoc=新的XmlDocument();
加载(@“C:\\shryas\\NX\u Temp\\NX\u Temp\\000048\u A\\CompareReport3D.xml”);
XmlNodeList nodeList=xmlDoc.DocumentElement.SelectNodes(“*”);
List returnedmatchNode=TraverseXml(节点列表);
对于(int-iIndex=0;iIndex
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>

Y
0,1,0,1,0,0,0,0,1

我试图显示returnedmatchNode的值。但它没有显示任何价值。我无法找出代码中的错误。

您总是实例化一个新的
列表,而不向其中添加新结果:

// this gets called in each round of your recursion
List<XmlNode> matchNode = new List<XmlNode>();


将递归调用更改为

matchNode.AddRange(TraverseXml(node1.ChildNodes, counter));
否则就不包括递归的结果


为了让运行您的代码的人保持清醒,请将
ReadLine()
移动到
Main

的末尾,在Visual Studio Debugger中运行您的程序,设置断点,单步执行代码,检查变量并查看它在哪里发生堆栈溢出!请提供一个,以便人们可以在自己的电脑上重现您的问题。在调用
TraverseXml
后,您是否验证了
returnedmatchNode
是否包含一个或多个值?您确定这不只是等待您按一次enter键,让每个节点都通过所有这些读取行吗?或者XML真的是tinyI检查了returnedmatchNode值。它没有给任何价值,我是这样运行的。到底是什么不起作用?我只是在你的代码中添加了
matchNode.AddRange(…)
,效果很好。你保持“List matchNode=new List();”不变吗?是的,其他一切都保持不变。除了我明确改变的。嘿,谢谢。我只需要卸下console.ReadLine();来自TraverseXml函数。嗯,
控制台.ReadLine
不会阻止它工作,它只是很乏味。下一次,如果你能告诉我们明显的错误是什么,而不是“它不工作”,这将是很有帮助的。
matchNode.AddRange(TraverseXml(node1.ChildNodes, counter));
matchNode.AddRange(TraverseXml(node1.ChildNodes, counter));