0
我需要讀取我的xml文件中的所有嵌套元素。以下是該計劃。在下面的程序中,我可以獲得問題和答案的值,但不能用於SubQuestionAnswer/Question和SubQuestionAnswer/Answer。有人能告訴我什麼是我的程序錯誤讀取和檢索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");
string questionanswer =string.Empty;
if (xnlNodes != null)
foreach (XmlNode xndNode in xnlNodes)
{
if (xndNode["Question"] != null)
questionanswer = (xndNode["Question"].InnerText);
if (xndNode["Answer"] != null)
questionanswer = (xndNode["Answer"].InnerText);
if (xndNode["SubQuestionAnswer/Question"] != null)
questionanswer = (xndNode["SubQuestionAnswer/Question"].InnerText);
if (xndNode["SubQuestionAnswer/Answer"] != null)
questionanswer = (xndNode["SubQuestionAnswer/Answer"].InnerText);
}
}