我有一個XML文件是這樣的:獲取的innerText
<locations>
<location country="UK">
<name>London</name>
<class>Silver</class>
</location>
<location country="Germany">
<name>Berlin II</name>
<class>Bronze</class>
</location>
</locations>
當打開一個網頁我扣除值「currentCountry」從URL。現在我想使用該值在XML文件中搜索相應的location
條目,並將其子節點的innerHtml值放入變量中。這是我當前的代碼,這是至今沒有工作:
string currentCountry = "Germany" //For testing purposes
string currentName = "";
string currentClass = "";
XmlDocument doc = new XmlDocument();
doc.Load("C:\\IIS\\Web\\Content\\locations.xml");
XmlNodeList xnList = doc.SelectNodes("/locations/location[@country='" + currentCountry + "']");
foreach (XmlNode xn in xnList)
{
currentName = xn.SelectSingleNode("/name").InnerText;
currentClass = xn.SelectSingleNode("/class").InnerText;
}
我得到的錯誤System.NullReferenceException: Object reference not set to an instance of an object.
指向foreach
循環中的第一道防線。
任何人都可以指出我正確的方向嗎?
所以你做了沒有研究,然後再發布? =( –
我做了研究,被卡住了,發佈了我的問題,並繼續我的研究。很幸運,我在發佈這裏後偶然發現瞭解決方案。 – Konadi