我的xml数据是:
<response>
<result name="response" numFound="2" start="0">
<doc><str name="Author">ALi Ahmadi</str><int name="BookID">1904</int><str name="Title">BookName 1</str></doc>
<doc><str name="Author">Reza Javadi</str><int name="BookID">1523</int><str name="Title">BookName 2</str></doc>
</result>
</response>
我的c#代码是:
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(ResultData);
XmlNodeList XnList = XmlDoc.SelectNodes("/response/result/doc");
foreach (XmlNode Xn in XnList)
{
string a = Xn["str"].InnerText;
string b = Xn["int"].InnerText;
string c = Xn["str"].InnerText;
//string c = Xn["str[@name='Title']"].InnerText;//error !
}
a&amp; c结果是:“Ali Ahmadi”,我怎样才能在c变量中获得“BookName 1”?
答案 0 :(得分:2)
如果您知道节点'Title'的索引 你可以通过这段代码得到它的inerrHTML:
c = Xn.ChildNodes[2].InnerText;
答案 1 :(得分:0)
试试这个
string c = Xn["str"].Attributes["Title"].Value