C# 读取和检索xml元素
我需要读取xml文件中的所有嵌套元素。下面是节目。在下面的程序中,我可以得到问题和答案的值,但不能得到子问题答案/问题和子问题答案/答案的值。有人能告诉我我的程序出了什么问题吗C# 读取和检索xml元素,c#,xml,C#,Xml,我需要读取xml文件中的所有嵌套元素。下面是节目。在下面的程序中,我可以得到问题和答案的值,但不能得到子问题答案/问题和子问题答案/答案的值。有人能告诉我我的程序出了什么问题吗 XmlDocument xmlDocument = new XmlDocument(); var fataQuestionnaire = @"<?xml version=""1.0"" encoding=""UTF-16""?> <FatcaQu
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);
}
}
XmlDocument XmlDocument=new XmlDocument();
var fataQuestionnaire=@”
你的财富来源是什么?
我是意大利人
你的职业和雇主的名字是什么?
贝斯廷维斯特
你在中国有生意或住所吗?
对
你在阿尔巴尼亚境外住了多久
五年
你定期回阿尔巴尼亚吗
对
多久一次?
每年
你在阿尔巴尼亚有家人吗?
对
家庭关系?
我父母住在那里
你与阿尔巴尼亚政府有联系吗?
对
结社性质
我是前部长
你从阿尔巴尼亚寄钱还是收到钱?
对
多久一次,为什么?
每个月我都要和父母住在一起。
";
XmlTextReader=新的XmlTextReader(新的StringReader(fataQuestionnaire));
加载(读卡器);
XmlElement xmlRoot=xmlDocument.DocumentElement;
if(xmlRoot!=null)
{
XmlNodeList xnlNodes=xmlRoot.SelectNodes(“/fatcaQuestionly/QuestionAnswers/QuestionAnswers”);
string questionanswer=string.Empty;
if(xnlNodes!=null)
foreach(xnlNodes中的XmlNode xndNode)
{
if(xndNode[“问题”]!=null)
questionanswer=(xndNode[“问题”].InnerText);
如果(xndNode[“应答”]!=null)
questionanswer=(xndNode[“答案”].InnerText);
if(xndNode[“子问题答案/问题”]!=null)
questionanswer=(xndNode[“子问题答案/问题”]。InnerText);
if(xndNode[“子问题答案/答案”]!=null)
问题答案=(xndNode[“子问题答案/答案”]。InnerText);
}
}
索引器不支持XPath语法。您可以通过以下方式进行修复:
if (xndNode["SubQuestionAnswer"] != null)
{
if (xndNode["SubQuestionAnswer"]["Question"] != null)
questionanswer = (xndNode["SubQuestionAnswer"]["Question"].InnerText);
if (xndNode["SubQuestionAnswer"]["Answer"] != null)
questionanswer = (xndNode["SubQuestionAnswer"]["Answer"].InnerText);
}
另一个选项:使用XDocument和Linq代替XmlDocument 加载xml:
XDocument doc = XDocument.Parse(fataQuestionnaire);
示例用法:
List<KeyValuePair<string,string>> allQuestionAnswers = doc.Descendants("QuestionAnswer").Union(doc.Descendants("SubQuestionAnswer"))
.Select(qa=>new KeyValuePair<string,string>(qa.Element("Question").Value, qa.Element("Answer").Value)).ToList();
foreach (var pair in allQuestionAnswers)
{
Console.WriteLine("Q: " + pair.Key +"\nA: " + pair.Value + "\n");
}
或:
结果:
Q: What is your occupation and name of employer?
A: Bestinvest
Q: Do you have a business or residence in?
A: Yes
Q: How long have you lived outside of Albania
A: 5 years
Q: Do you return to Albania on a regular basis
A: Yes
Q: Do you have family in Albania?
A: Yes
Q: Are you connected to the government of Albania?
A: Yes
Q: Do you send or receive money from Albania?
A: Yes
Q: How frequently?
A: every year
Q: Family relationship?
A: My parents lives there
Q: Nature of association
A: I was an ex minister
Q: How often and why?
A: Every month for my parents to live with.
Q: What is your source of wealth?
A: I am italian
Q: What is your occupation and name of employer?
A: Bestinvest
Q: Do you have a business or residence in?
A: Yes
Q: How long have you lived outside of Albania
A: 5 years
Q: Do you return to Albania on a regular basis
A: Yes
Q: How frequently?
A: every year
Q: Do you have family in Albania?
A: Yes
Q: Family relationship?
A: My parents lives there
Q: Are you connected to the government of Albania?
A: Yes
Q: Nature of association
A: I was an ex minister
Q: Do you send or receive money from Albania?
A: Yes
Q: How often and why?
A: Every month for my parents to live with.
Q: What is your source of wealth?
A: I am italian
Q: What is your occupation and name of employer?
A: Bestinvest
Q: Do you have a business or residence in?
A: Yes
Q: How long have you lived outside of Albania
A: 5 years
Q: Do you return to Albania on a regular basis
A: Yes
Q: How frequently?
A: every year
Q: Do you have family in Albania?
A: Yes
Q: Family relationship?
A: My parents lives there
Q: Are you connected to the government of Albania?
A: Yes
Q: Nature of association
A: I was an ex minister
Q: Do you send or receive money from Albania?
A: Yes
Q: How often and why?
A: Every month for my parents to live with.