0

我正在使用 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 和答案值?

谢谢,

查图尔

4

1 回答 1

0

不是 ANSWER,但包括在此处而不是在注释中以使 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 所述,您的实际问题的答案已经在本网站的其他地方。

于 2012-11-26T13:14:56.867 回答