2012-11-26 66 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如上所述,您的實際問題的答案已經在本網站的其他地方。