C#中的简单XML读取无效

时间:2011-10-21 05:33:17

标签: c# xml

这是我正在尝试阅读的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语句。证明,消息框“test”永远不会运行。其他一切都很好。数组调整大小也很好(我在它后面放了一个消息框来查看程序是否到达那里)。你们能指出这个错误吗?

好吧,我在亚当答案的帮助下解决了这个问题。这是代码

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);

2 个答案:

答案 0 :(得分:6)

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))填充完全类型的模型。

答案 1 :(得分:1)

为什么你甚至关心检查终端节点?一旦调用ReadInnerXml,就已经在当前元素的末尾。在最后想要使用的块中移动“无论代码是什么”,只需在读取内部xml后包含它。