2014-10-28 59 views
0

我需要讀取xml字符串並將值分配給列表集合。我需要讀取問題節點並將其分配給列表收集變量。同樣閱讀答案並將其分配給列表收集變量。目前,問題和答案正在被覆蓋,而不是遍歷到下一個節點。有人能告訴我問題是什麼嗎?讀取xml字符串和填充列表集合

以下是代碼

XmlDocument xmlDocument = new XmlDocument(); 

      var fataQuestionnaire = @"<?xml version=""1.0"" encoding=""UTF-16""?> 
          <FatcaQuestionnaire xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> 
          <QuestionAnswers> 
            <QuestionAnswer> 
            <Question>What is your source of wealth?</Question> 
            <Answer>I am italian </Answer> 
            </QuestionAnswer> 
            <QuestionAnswer> 
            <Question>What is your occupation and name of employer?</Question> 
            <Answer>Bestinvest</Answer> 
            </QuestionAnswer> 
            <QuestionAnswer> 
            <Question>Do you have a business or residence in?</Question> 
            <Answer>Yes</Answer> 
            </QuestionAnswer> 
            <QuestionAnswer> 
            <Question>How long have you lived outside of Albania</Question> 
            <Answer>5 years</Answer> 
            </QuestionAnswer> 
            <QuestionAnswer> 
            <Question>Do you return to Albania on a regular basis</Question> 
            <Answer>Yes</Answer> 
            <SubQuestionAnswer> 
             <Question>How frequently?</Question> 
             <Answer>every year</Answer> 
            </SubQuestionAnswer> 
            </QuestionAnswer> 
            <QuestionAnswer> 
            <Question>Do you have family in Albania?</Question> 
            <Answer>Yes</Answer> 
            <SubQuestionAnswer> 
             <Question>Family relationship?</Question> 
             <Answer>My parents lives there</Answer> 
            </SubQuestionAnswer> 
            </QuestionAnswer> 
            <QuestionAnswer> 
            <Question>Are you connected to the government of Albania?</Question> 
            <Answer>Yes</Answer> 
            <SubQuestionAnswer> 
             <Question>Nature of association</Question> 
              <Answer>I was an ex minister</Answer> 
            </SubQuestionAnswer> 
            </QuestionAnswer> 
            <QuestionAnswer> 
            <Question>Do you send or receive money from Albania?</Question> 
            <Answer>Yes</Answer> 
            <SubQuestionAnswer> 
             <Question>How often and why?</Question> 
             <Answer>Every month for my parents to live with.</Answer> 
            </SubQuestionAnswer> 
            </QuestionAnswer> 
          </QuestionAnswers> 
          </FatcaQuestionnaire>"; 

      XmlTextReader reader = new XmlTextReader(new StringReader(fataQuestionnaire)); 


      xmlDocument.Load(reader); 

      XmlElement xmlRoot = xmlDocument.DocumentElement; 
      if (xmlRoot != null) 
      { 
       XmlNodeList xnlNodes = xmlRoot.SelectNodes("/FatcaQuestionnaire/QuestionAnswers/QuestionAnswer"); 
       List<string> questionanswer = new List<string>(); 

      if (xnlNodes != null) 
       foreach (XmlNode xndNode in xnlNodes) 
       { 
        if (xndNode["Question"] != null) 
         questionanswer[0] = xndNode["Question"].InnerText; 

        if (xndNode["Answer"] != null) 
         questionanswer[1] = xndNode["Answer"].InnerText; 

        if (xndNode["Question"] != null) 
         questionanswer[2] = xndNode["Question"].InnerText; 

        if (xndNode["Answer"] != null) 
         questionanswer[3] = xndNode["Answer"].InnerText; 
       } 

       } 
      } 
+0

你的樣品沒有太大的意義 - 你正試圖將值設置爲不存在的元素'列表「(如'questionanswer [2] = ...') - 請確保代碼至少看起來像可以工作的代碼。還要考慮調試以提供更多細節。 – 2014-10-28 23:22:58

回答

0

替換:

foreach (XmlNode xndNode in xnlNodes) 
{ 
    if (xndNode["Question"] != null) 
    questionanswer[0] = xndNode["Question"].InnerText; 

    if (xndNode["Answer"] != null) 
    questionanswer[1] = xndNode["Answer"].InnerText; 

    if (xndNode["Question"] != null) 
    questionanswer[2] = xndNode["Question"].InnerText; 

    if (xndNode["Answer"] != null) 
    questionanswer[3] = xndNode["Answer"].InnerText; 
} 

foreach (XmlNode xndNode in xnlNodes) 
{ 
    if (xndNode["Question"] != null) 
    questionanswer.Add(xndNode["Question"].InnerText) 

    if (xndNode["Answer"] != null) 
    questionanswer.Add(xndNode["Answer"].InnerText); 
} 

您需要添加到列表中。

0

當您第一次創建列表時它是空的,因此它沒有要添加的對象。您正試圖將值設置爲數組中不存在的字段。

所以: 更改此代碼:

foreach (XmlNode xndNode in xnlNodes) 
{ 
    if (xndNode["Question"] != null) 
    questionanswer[0] = xndNode["Question"].InnerText; 

    if (xndNode["Answer"] != null) 
    questionanswer[1] = xndNode["Answer"].InnerText; 

    if (xndNode["Question"] != null) 
    questionanswer[2] = xndNode["Question"].InnerText; 

    if (xndNode["Answer"] != null) 
    questionanswer[3] = xndNode["Answer"].InnerText; 
} 

要這樣:

foreach (XmlNode xndNode in xnlNodes) 
{ 
    if (xndNode["Question"] != null) 
    questionanswer.Add(xndNode["Question"].InnerText); 

    if (xndNode["Answer"] != null) 
    questionanswer.Add(xndNode["Answer"].InnerText); 

}