c# - C# 中的简单 XML 读取不起作用

标签 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 语句。证明,消息框“测试”从未运行过。其他一切都很好。数组大小调整也很好(我在它后面放了一个消息框,看看程序是否到达那里)。你们能指出错误吗?

好吧,我在 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/

相关文章:

C# 如何强制在双显示器系统的主显示器中显示启动画面?

c# - SQL 用户类型问题

c# - WPF 媒体元素的背景图像

mysql - 嵌套 XML 以加入 MySQL 表

java - 如何使用 JAXB 忽略绑定(bind)到 XML 的字段

xml - 如何实例化抽象类型的 XSD 元素

c# - 在初始页面加载时从服务器获取类对象并将其放入 js 对象的好方法是什么?

c# - 该程序集不允许部分受信任的调用方。初始化组件()

java - 转换 XML 中的任何 ActionScript 类

java - 如何使用 ImageButton 从一个 fragment 更改为另一个 fragment ?