2012-11-26 51 views
0

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

我只需要提取问题ID和相关的答案ID(s)。示例XML如下。根据答案的类型(单选,多选项或文本框)Question_QuestionID_SomeLogic:

<?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在格式生成,

标签名称格式。

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

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

感谢,

Chatur

+0

好的先生,我确信这是以下问题的重复。请记住在发布之前先搜索一个问题。 http://stackoverflow.com/questions/2947738/how-to-read-xml-nodes-in-xml-using-c-net – TheGeekZn

+4

不是anwser,而是谁生成的XML?恕我直言,这是非常糟糕的格式。如果你是程序员,你应该考虑改变结构 –

+0

你可以正则表达式元素的名字,但通常这是错误的方式来形成XML,如果可能的话,首先要改变:) – Giedrius

回答

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如上所述,您的实际问题的答案已经在本网站的其他地方。