我无法从控制台输入的值中列出正确的值。我的 XML 文件是:
<Students>
<Student>
<Name>Name1</Name>
<Surname>Surname1</Surname>
<Index>2222</Index>
<Subject name="History">
<Class>Class2</Class>
<Status status="passed">
<Grade>A</Grade>
</Status>
</Subject>
</Student>
<Student>
<Name>Name2</Name>
<Surname>Surname2</Surname>
<Index>3333</Index>
<Subject name="Math">
<Class>Class3</Class>
<Status status="passed">
<Grade>D</Grade>
</Status>
</Subject>
</Student>
</Students>
所以我想做的是当我输入例如 3333 时,我想列出学生所属的班级,在这种情况下是“Class3”。我的代码是这样的:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Students.xml");
Console.WriteLine("\nInsert Index Number");
string result = Console.ReadLine();
XmlNodeList xnList = xmlDoc.SelectNodes("//Student/Index");
XmlNodeList xnList2 = xmlDoc.SelectNodes("//Student/Subject/Class");
string result2 = null;
for (int i = 0; i < xnList.Count; i++)
{
string nodeval = xnList[i].InnerText;
if (nodeval == result)
for (int j = 0; j < xnList2.Count; j++)
{
result2 = xnList2[j].InnerText;
Console.WriteLine("Result" + result2);
}
}
}
有什么帮助吗?谢谢