xml解析?如何? [英] xml parsing ? how to?

查看:46
本文介绍了xml解析?如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将这个xml数据的Question元素解析成一个名为question的对象,其中包含一个名为options的字符串数组属性?我知道有很多xml帖子都有答案,但他们都让我困惑,我只需要一个适合我的问题。 xml文件中总共有50个问题,我想将每个问题元素及其子元素和内容提取到问题对象中。我在visual studio 2010上使用c#



how do i parse this xml data''s "Question" element into an object called "question" with a string array property called "options"? I know there are lots of xml posts with answers but they all confuse me i just need one that fits my problem. There will be a total of 50 questions in the xml file, and i want to extract each question element and it''s child elements and contents into a question object. i use c# on visual studio 2010

<?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>

推荐答案

您需要的是XML反序列化。你可以从这里开始: http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization [ ^ ]。如果您需要更多,这里有一篇非常好的文章和工具: A Deep XmlSerializer,支持复杂类,枚举,结构,集合和数组 [ ^ ]
What you need is called XML deserialization. You can start here: http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization[^]. If you need more, here is a really good article and tool: A Deep XmlSerializer, Supporting Complex Classes, Enumerations, Structs, Collections, and Arrays[^]


这篇关于xml解析?如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆