这是我试图从中读取的 XML:
<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>
这是我要运行的代码:
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;
}
}
}
}
}
不知何故,程序永远不会进入最后一个 if 语句。证明,消息框“测试”从未运行过。其他一切都很好。数组大小调整也很好(我在它后面放了一个消息框,看看程序是否到达那里)。你们能指出错误吗?
好吧,我在 Adam 的回答的帮助下解决了这个问题。这是代码
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);
最佳答案
ReadInnerXml()
导致内容被消耗;这就是为什么您什么都看不到的原因。
除非 xml 很大,否则我会向 XmlReader
推荐几乎任何其他 xml 处理 - 它不是为随意使用而设计的。建议:
XmlSerializer
(将数据加载到类型化对象中)XmlDocument
(将数据加载到 DOM 中)XDocument
(相同,但使用 LINQ-to-XML)
要使用 XmlSerializer 执行此操作:
[XmlRoot("events")]
public class Events {
[XmlElement("event")]
public List<Event> Items {get;set;}
}
public class Answer {
[XmlAttribute("cost")]
public int Cost {get;set;}
[XmlText]
public string Text {get;set;}
}
public class Event {
[XmlElement("answer")]
public List<Answer> Answers {get;set;}
[XmlElement("text")]
public string Text {get;set;}
}
然后您可以使用 new XmlSerializer(typeof(Events))
来填充完全类型化的模型。
关于c# - C# 中的简单 XML 读取不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7845347/