Reputation: 236
I am having difficulties with my XML code it doesn't seem to saving and when I print it out nothing happens. I am not sure what is wrong because before it would load into my listbox but it would load incorrectly. The code is below and the purpose of my XML reading is the store the values in a list and then get a selected tag and add it to a listbox.
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);
}
}
Upvotes: 1
Views: 144
Reputation: 2871
I think the most likely reason is that in each of your if statements, you are using textReader.Read()
. For most Reader
s this will read the next item, not the current.
As the other answer has said, you need to look at the element for the Name
and then read for the value.
Consider something like this instead:
while (textReader.Read())
{
textReader.MoveToElement();
type = textReader.NodeType;
if (type == XmlNodeType.Element)
{
textReader.Read();
switch( textReader.Name )
{
case "Code":
code = textReader.Value;
break;
case "Name":
name = textReader.Value;
break;
//SNIP
case "Info":
info = textReader.Value;
module.Add(new modules(name, code, semester, tSlot, lSlot, info, preReq));
break;
default:
//Whatever you do here
break;
}
Console.WriteLine(textReader.Value);
}
foreach (object o in module)
{
modules m = (modules)o;
String hold = m.mName;
selectionBox.Items.Add(hold);
}
}
This way your XMLTextReader is only reading one node per iteration, and you have a lot fewer if checks - this is the situation a switch case was designed for.
Upvotes: 0
Reputation: 47058
The thing is that you look for type == XmlNodeType.Text
, but text nodes does not have any name, no text nodes will match textReader.Name == "Code"
.
You need to store textReader.Name
from the last node with type == XmlNodeType.Element
in a variable and use the stored name when you find the XmlNodeType.Text
node.
Upvotes: 2