C#使用动态生成的标签读取XML文件

时间:2012-11-26 12:47:30

标签: c# xml xml-parsing

我正在使用C#(带有VS 2010的.Net框架4)来读取动态生成的XML文件。该文件包含问题的答案(MCQ单选按钮,MCQ多答复选框和简单文本字段)。问题ID和选项ID是从数据库生成的。

我只需要提取问题ID和相关的答案ID。示例XML如下。

<?xml version="1.0"?>
<Root>
   <!-- Radio Button Answers -->    
   <Question_6_Option>26</Question_6_Option>
   <Question_8_Option>32</Question_8_Option>
   <Question_9_Option>off</Question_9_Option>
   <!-- Check Box Answers -->
   <Question_15_Option_41>41</Question_15_Option_41>
   <Question_15_Option_42>off</Question_15_Option_42>
   <Question_16_Option_43>43</Question_16_Option_43>
   <!-- Text Box Answers -->
   <Question_23_Text>London</Question_23_Text>
</Root>

以上XML是以格式

生成的

标记名称格式:Question_QuestionID_SomeLogic基于答案类型(广播,多个选项或文本框)。

如果未回答问题值的用户将显示为“关闭”。那些没有必要考虑。

如何从C#获取问题ID和答案值?

谢谢,

Chatur

1 个答案:

答案 0 :(得分:0)

不是答案,但包含在此处而非评论中,以使XML更具可读性:

我建议如果你能让你的XML更具有计算机可读性 - 即从标签名中删除数据并将其放入属性中,以便更容易解析。

<?xml version="1.0"?>
<Root>
    <!-- Radio Button Answers -->    
    <Question number="6" type="radio"><Selected index="26" /></Question>
    <Question number="8" type="radio"><Selected index="32" /></Question>
    <Question number="9" type="radio" />
    <!-- Check Box Answers -->
    <Question number="15" type="check">
        <Selected index="41" />
        <Selected index="42" /><!-- not sure if this should be included, I didn't understand how question 15 was both off (unanswered) and 41 (answered) -->
    </Question>
    <Question number="16" type="check">
        <Selected index="43" />
    </Question>
    <!-- Text Box Answers -->
    <Question number="23" type="text">London</Question>
</Root>

正如@NewAmbition所述,您实际问题的答案已经在本网站的其他地方。