我有解析XML文件的問題。我設法使用xpath讀取一些示例xml文件,但我無法讀取我的XML文件。無論我輸入爲xpath表達式,它都無法解析文件。無法使用Xpath解析XML
這裏是xml文件我想讀
<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>dino</name>
<SSIDConfig>
<SSID>
<hex>64696E6F</hex>
<name>dino</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<MSM>
<security>
<authEncryption>
<authentication>open</authentication>
<encryption>WEP</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>networkKey</keyType>
<protected>true</protected>
<keyMaterial>01000000D08C9DDF0115D1118C7A00C04FC297EB010000000C046798E62AF24993A8197E7DBF7BDC00000000020000000000106600000001000020000000E64B73274BEAB7199AB89DB1CFED12146D88E9B5EF36823E780F33E9B67E2CDC000000000E80000000020000200000002BF0125DD33F628A7D7B1C867298C62DEF53C6712E8ED4051491F317B0A1710A100000003748D54F3C408A3E076006C03174B10440000000BF822418926AF8663A6FE9619C34C765009EB9D8B23104FEBE049EB1E5B96F8BD61D3E8056885958616E5C50CE9FDF6DC4517B4405C0C45EA392327964301508</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>
例如,我想keyMaterial。這是密碼
string fileloc = String.Format("{0}/dino.xml", destination);
label3.Text = fileloc; // path check
XPathDocument doc = new XPathDocument(fileloc);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr;
expr = nav.Compile("/WLANProfile/MSM/security/sharedKey/keyMaterial");
XPathNodeIterator iterator = nav.Select(expr);
listBox1.Items.Clear();
try
{
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
listBox1.Items.Add("keyMaterial: " + nav2.Value);
}
}
catch (Exception ex)
{
label2.Text = ex.Message;
那麼,問題在哪裏呢?無論我輸入爲nav.Compile,listBox都是空的。
你爲什麼不使用'XDocument'呢? – Oded 2012-03-22 15:06:40
我想用xPath。如果我們不能解析它,那麼我將使用XDocument。 – nighthawk 2012-03-22 15:34:54
嘗試將名稱空間添加到表達式。它看起來像缺乏命名空間的問題 – Marcin 2012-03-22 16:41:49