Here’s my XML from which i’m trying to read:
<events>
<event>
<text>You tire has a hole.</text>
<answer cost="50">patch it</answer>
<answer cost="100">replace it</answer>
<answer cost="0">use your spare tire</answer>
</event>
<event>
<text>It's your friend's birthday, everyone's going out to a fancy restaurant.</text>
<answer cost="60">go to the restaurant</answer>
<answer cost="30">go to the restaurant, but order something really cheap</answer>
<answer cost="0">don't go</answer>
</event>
<event>
<text>Your winter coat's zipper is damaged. Replacing it is costly.</text>
<answer cost="50">replace it</answer>
<answer cost="0">leave it like that</answer>
</event>
This is the code i’m trying to run:
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "text")
{
MessageBox.Show(reader.ReadInnerXml());
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "answer")
{
anEvent.costs[anEvent.costs.Length - 1] = Convert.ToInt32(reader.GetAttribute("cost"));
anEvent.choices[anEvent.choices.Length - 1] = reader.ReadInnerXml();
Array.Resize(ref anEvent.choices, anEvent.choices.Length + 1);
Array.Resize(ref anEvent.costs, anEvent.costs.Length + 1);
if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "event")
{
MessageBox.Show("test");
eventsList.Add(anEvent);
anEvent = new Event();
break;
}
}
}
}
}
somehow, the program never gets into the last if statement. Proof, the message box “test” never runs. Everything else is fine. The array resizes are fine too (i put a message box after it to see if the program gets there). Can you guys pinpoint the mistake?
well, I fixed the problem with the help of Adam’s answer. Here’s the code
XmlReader reader = XmlReader.Create("dentist_events.xml");
bool isNotFirst = false;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "text")
{
if (isNotFirst)
{
Array.Resize(ref anEvent.choices, anEvent.choices.Length - 1);
Array.Resize(ref anEvent.costs, anEvent.costs.Length - 1);
eventsList.Add(anEvent);
anEvent = new Event();
//MessageBox.Show(reader.ReadInnerXml());
anEvent.eventText = reader.ReadInnerXml();
}
else
{
//MessageBox.Show(reader.ReadInnerXml());
anEvent.eventText = reader.ReadInnerXml();
}
}
else if (reader.NodeType == XmlNodeType.Element && reader.Name == "answer")
{
isNotFirst = true;
//MessageBox.Show(reader.GetAttribute("cost"));
//MessageBox.Show(reader.ReadInnerXml());
anEvent.costs[anEvent.costs.Length - 1] = Convert.ToInt32(reader.GetAttribute("cost"));
anEvent.choices[anEvent.choices.Length - 1] = reader.ReadInnerXml();
Array.Resize(ref anEvent.choices, anEvent.choices.Length + 1);
Array.Resize(ref anEvent.costs, anEvent.costs.Length + 1);
}
}
Array.Resize(ref anEvent.choices, anEvent.choices.Length - 1);
Array.Resize(ref anEvent.costs, anEvent.costs.Length - 1);
eventsList.Add(anEvent);
Why do you even care about checking the end node? Once you call ReadInnerXml you are already at the end of the current element. Move ‘whatever that code is’ in the block you want to use at the end and simply include it after you’ve read the inner xml.