你好我试图将一些数据从动态文本框保存到XML文件中。但每次我尝试我都会收到此错误
"指数超出范围。必须是非负数且小于集合的大小。 参数名称:index"
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();
帮助赞赏:)
答案 0 :(得分:2)
textboxComputer
似乎是一个数组,所以在你的for循环中执行:
for( int i = 0; i < textboxComputer.Length; i++ ) ...
或者你可以预先计算它:
int length = textboxComputer.Length;
for( int i = 0; i < length; i++ ) ...