2012-06-18 95 views
0

我有以下XML並希望返回所有的「答案」兒童名單返回具有相同名稱的多個XML兒童LINQ

<quiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="quiz.xsd"> 
    <mchoice> 
     <question>What is the capital city of Australia?</question> 
     <answer>Sydney</answer> 
     <answer correct="yes">Canberra</answer> 
     <answer>Melbourne</answer> 
     <answer>Gold Coast</answer> 
    </mchoice> 
    <mchoice> 
     <question>Launceston is the second largest city in which Australian state?</question> 
     <answer>Victoria</answer> 
     <answer>New South Wales</answer> 
     <answer correct="yes">Tasmania</answer> 
     <answer>Western Australia</answer> 
    </mchoice> 
</quiz> 



public class Question 
{ 
    public string QuestionText { get; set; } 
    public List<Answer> Answers { get; set; } 

} 
public class Answer 
{ 
    public string Answer1 { get; set; } 
    public string Answer2 { get; set; } 
    public string Answer3 { get; set; } 
    public string Answer4 { get; set; } 
} 

我嘗試以下LINQ查詢,但我被困在回答現場

public IEnumerable<Question> GetAll() 
    { 
     var questions = from docs in _doc.Descendants("mchoice") 
         select new 
         { 
          QuestionText = docs.Element("question").Value, 
          Answers = docs.Descendants("answer").SelectMany(e=>e.Element("answer").Value) 

         }; 
     return questions; 

    } 

回答

1

更改您的類作爲

public class Question 
{ 
    public string QuestionText { get; set; } 
    public List<string> Answers { get; set; } 

} 

然後將查詢將

var questions = from docs in _doc.Descendants("mchoice") 
         select new Question 
         { 
          QuestionText = docs.Element("question").Value, 
          Answers = docs.Elements("answer").Select(a=>a.Value).ToList() 

         }; 
+0

+1替代solution.your解決方案工作正常 –

+0

在這種情況下可以使用Answer類嗎? –

+0

是的,但是你將無法使用循環,並且必須在編碼時一直寫* answer1 *,* answer2 *。 –

0

嘗試修改此:

Answers1 = docs.Descendants("answer").SelectMany(e=>e.Element("answer").Value)

爲此:

Answers1 = docs.Descendants("answer").SelectMany(e=>e.Value)

+0

究竟發生了什麼? –

+0

我收到以下錯誤無法隱式轉換類型'System.Collections.Generic.IEnumerable '到 –

1

我不太清楚你從邏輯結構實體是正確的。您似乎有一個類型爲List的Answers屬性,每個答案實體又有4個(總是4個?)答案!

我想,改變它這樣會更有意義。

public class Question 
{ 
    public string QuestionText { get; set; } 
    public List<Answer> Answers { get; set; } 

    public Question() 
    { 
     if (Answers == null) 
      Answers = new List<Answer>(); 
    } 

} 
public class Answer 
{ 
    public string Answer1 { get; set; } 
} 

,你可以寫通過讀您的XML

public static IEnumerable<Question> GetAll(XElement elm) 
{ 
    var allQA = new List<Question>(); 
    var mchoices = elm.Descendants("mchoice").ToList(); 
    foreach (var choice in mchoices) 
    { 
     var answers = choice.Descendants("answer").ToList(); 
     var qA = new Question { QuestionText = choice.Descendants("question").SingleOrDefault().Value }; 
     foreach (var answer in answers) 
     { 
      qA.Answers.Add(new Answer { Answer1 = answer.Value}); 
     } 
     allQA.Add(qA);    
    } 
    return allQA; 
} 

返回問題(附答案)的列表的方法,我可以隨時調用它像這樣的地方,我想

XElement elm = XElement.Load(Server.MapPath(@"../YourFolder/sample.xml")); 
//You can alternatively load from string/stream etc.. 
if (elm != null) 
{ 
    var questionList=GetAll(elm); 
} 
+0

+1瞭解詳細解決方案 –

相關問題