开发者

Stack Overflow in XmlReader C#

this code throws me an error. I explain the error in the code:

public partial class Util
{
    public string LoadFunctions()
    {

        string codeFunctionsString = "";

        XmlReader reader = XmlReader.Create("fname2.xml");
        //The line above throws an error:
        //"An unhandled exception of type 'System.StackOverflowException' occ开发者_开发技巧urred in mscorlib.dll"
        //The file DOES exist, so I don't know what the problem is.

        reader.Read();
        while (reader.Read())
        {

            reader.ReadToFollowing("item");
            //reader.
            codeFunctionsString += reader.Value + "|";

            Form1 win = new Form1();

            win.CodeInput.Text += reader.Value + " ";

        }
        return codeFunctionsString;
    }
}


Based on your reply to my comment-based question, here's what's going on.

class Form1 : Form {
    public Form1() {
        InitializeComponents();

        Util u = new Util();
        string functions = u.LoadFunctions();
    }
}

In this case, I'm guessing that the call is in either the constructor (as above), or in Form1_Load (not shown, but same idea).

Well, what happens is that when you call LoadFunctions, it creates a Form1, which calls LoadFunctions, which creates a Form1, which calls LoadFunctions, which creates a Form1, which calls LoadFunctions, which creates a Form1, which calls LoadFunctions, which creates a Form1, which calls LoadFunctions, which creates a Form1, which-ERROR: StackOverflowException

The resolution is to either not create a form in LoadFunctions (perhaps, make it an argument you pass in?), or don't call LoadFunctions in Form1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜