我的 XML 代码遇到问题,它似乎没有保存,当我打印出来时,什么也没有发生。我不确定出了什么问题,因为在它加载到我的列表框之前,它会加载不正确。代码如下,我读取 XML 的目的是将值存储在列表中,然后获取选定的标签并将其添加到列表框中。
String workingDir = Directory.GetCurrentDirectory();
XmlTextReader textReader = new XmlTextReader(workingDir + @"\XML.xml");
textReader.Read();
XmlNodeType type;
while (textReader.Read())
{
textReader.MoveToElement();
type = textReader.NodeType;
if (type == XmlNodeType.Text)
{
if (textReader.Name == "Code")
{
textReader.Read();
code = textReader.Value;
Console.WriteLine(code);
}
if (textReader.Name == "Name")
{
textReader.Read();
name = textReader.Value;
Console.WriteLine(name);
}
if (textReader.Name == "Semester")
{
textReader.Read();
semester = textReader.Value;
Console.WriteLine(semester);
}
if (textReader.Name == "Prerequisite")
{
textReader.Read();
preReq = textReader.Value;
Console.WriteLine(code);
}
if (textReader.Name == "LectureSlot")
{
textReader.Read();
lSlot = textReader.Value;
Console.WriteLine(lSlot);
}
if (textReader.Name == "TutorialSlot")
{
textReader.Read();
tSlot = textReader.Value;
Console.WriteLine(tSlot);
}
if (textReader.Name == "Info")
{
textReader.Read();
info = textReader.Value;
module.Add(new modules(name, code, semester, tSlot, lSlot, info, preReq));
}
}
foreach (object o in module)
{
modules m = (modules)o;
String hold = m.mName;
selectionBox.Items.Add(hold);
}
}