0

您好,我正在尝试将动态文本框中的一些数据保存到 XML 文件中。但每次我尝试我都会收到这个错误

“索引超出范围。必须为非负数且小于集合的大小。参数名称:索引”

XmlDocument document = new XmlDocument();
                document.Load(@"myGrades.xml");            
                for (int i = 0; i < 7; i++)
                {
                    **TextBox tb1 = (TextBox)textboxComputer[i];** //<- error is thrown here
                    string markGained = tb1.Text;
                    this.Text = markGained;
                    XmlElement root = document.DocumentElement;
                    XmlElement newAssessmentMark = document.CreateElement("Assessment");
                    XmlElement newMark = document.CreateElement("AssessmentMark");
                    XmlText mark = document.CreateTextNode(markGained);
                    newAssessmentMark.AppendChild(newMark);
                    newMark.AppendChild(mark);
                    XmlNode parentNode = document.SelectSingleNode("myGrades/courseStructure/level4");
                    parentNode.InsertAfter(newAssessmentMark, parentNode.LastChild);

                    document.Save(@"myGrades.xml");

                }

                this.Close();

帮助表示赞赏:)

4

1 回答 1

2

textboxComputer似乎是一个数组,所以在你的 for 循环中:

for( int i = 0; i < textboxComputer.Length; i++ ) ...

或者,您可以预先计算它:

int length = textboxComputer.Length;

for( int i = 0; i < length; i++ ) ...

于 2012-05-11T09:58:46.190 回答