【问题标题】:xml parsing? how to? [closed]xml解析?如何? [关闭]
【发布时间】:2012-12-12 16:48:11
【问题描述】:

如何将这个 xml 数据的“问题”元素解析为一个名为“问题”的对象,该对象具有一个名为“选项”的字符串数组属性?我知道有很多带有答案的 xml 帖子,但它们都让我感到困惑,我只需要一个适合我的问题的帖子。 xml 文件中总共有 50 个问题,我想将每个问题元素及其子元素和内容提取到一个问题对象中。我在 Visual Studio 2010 上使用 C#。

<?xml version="1.0" encoding="utf-8" ?>
<Questions>
 <Question id ="1">
<Content>Which of the following statements represents the view expressed by the writer    in the first paragraph?</Content>
<Options>
  <A>Evil Thoughts will eventually ruin the evil man.</A>
  <B>If we do not stop the pendulum of thoughts from swinging, our thoughts will soon become our enemies.</B>
  <C>Too many evil thoughts leave fatal consequence.</C>
  <D>It is possible to decide what controls our thoughts.</D>
</Options>
</Question>
<Question id ="2">
<Content>From the argument in the second paragraph, it can be concluded that evil thoughts control the lives people who</Content>
<Options>
  <A>are helpless because they fly out of their minds</A>
  <B>cherish idle and slothful ways</B>
  <C>are thieves with evil instincts</C>
  <D>treasure and ruminate on them.</D>
</Options>
</Question>
<Question id ="3">
   <Content>The expression think of the devil and he will appear..., as used in this  passage, suggests that</Content>
 <Options>
  <A>like the devil, evil thoughts must not reign in our hearts</A>
  <B>evil thoughts are fantasies which exist only in people's minds</B>
  <C>uncontrolled evil thoughts may lead to evil deeds</C>
  <D>the devil gives evil thoughts only to those who invite him in.</D>
</Options>
</Question>
<Question id ="4">
<Content>Which of the following statements summarizes the argument of the last paragraph?</Content>
 <Options>
  <A>Heavy traffic on a miry and dirty road may lead to evil thoughts.</A>
  <B>The more evil we think, the more vile we are likely to become.</B>
  <C>Evil people should not be welcomed as guest in our homes the same way as we welcome good people.</C>
  <D>Evil thoughts control the key to the human heart and no one can keep the out.</D>
</Options>

【问题讨论】:

  • 花一点时间了解序列化的工作原理意味着这些帖子不会再让您感到困惑。
  • 你尝试过什么..hireacoder 或者你自己做,如果你有任何问题可以回来这里..!

标签: c# xml linq linq-to-xml


【解决方案1】:

您可以使用LINQ to Xml

XDocument xdoc = XDocument.Load(path_to_xml);
var query = xdoc.Descendants("Question")
                .Select(q => new Question()
                {
                    Id = (int)q.Attribute("id"),
                    Content = (string)q.Element("Content"),
                    Options = q.Element("Options")
                               .Elements()
                               .Select(o => (string)o).ToArray()
                });

这将返回IEnumerable&lt;Question&gt; 序列,其中 Question 是一个类似的类:

public class Question
{
    public int Id { get; set; }
    public string Content { get; set; }
    public string[] Options { get; set; }
}

【讨论】:

  • 感谢您的回答,不要认为我的问题是愚蠢和重复的
猜你喜欢
  • 2012-03-04
  • 1970-01-01
  • 2018-06-18
  • 2013-06-07
  • 1970-01-01
  • 1970-01-01
  • 2013-10-22
相关资源
最近更新 更多