Reputation: 832
i have problem parsing XML files. I managed to read some sample xml files using xpath but i cannot read my XML file. Whatever i enter as xpath expression, it cannot parse the file.
Here is the xml file i am trying to read
<?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>
For example, i want to get keyMaterial. This is the code
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;
So, where is the problem? listBox is empty whatever i enter as nav.Compile.
Upvotes: 0
Views: 614
Reputation: 3262
Try following with your code, I've checked and it is working:
XPathDocument doc = new XPathDocument(fileloc);
XPathNavigator nav = doc.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
manager.AddNamespace("bk", "http://www.microsoft.com/networking/WLAN/profile/v1");
XPathNodeIterator iterator = nav.Select("/bk:WLANProfile/bk:MSM/bk:security/bk:sharedKey/bk:keyMaterial", manager);
Upvotes: 1
Reputation: 116098
If you want to use XDocument
XDocument xDoc = XDocument.Load(.....);
XNamespace ns = XNamespace.Get("http://www.microsoft.com/networking/WLAN/profile/v1");
var key = xDoc.Descendants(ns+"keyMaterial").First();
Upvotes: 1