[英]Reading an XML document with XMLDocument C# 'password' string not returning
I'm trying to read the text in between <keyMaterial></keyMaterial>
我正在尝试阅读
<keyMaterial></keyMaterial>
之间的文本
I tried using //WLANProfile/MSM/security/sharedKey
as the element route, seen in the code below. 我尝试使用
//WLANProfile/MSM/security/sharedKey
作为元素路由,如以下代码所示。 It refuses to return a value. 它拒绝返回值。 I have run through the debugger and after the breakpoint at the line:
XmlNodeList sharedKeyNodes = wifiProfile.SelectNodes("//WLANProfile/MSM/security/sharedKey");
我已经通过调试器并在该行的断点之后运行了:
XmlNodeList sharedKeyNodes = wifiProfile.SelectNodes("//WLANProfile/MSM/security/sharedKey");
the SharedKeyNodes object doesn't return a count for. SharedKeyNodes对象不返回计数。 I know it's just a matter of figuring out the element route so I'm not coming here completely hopeless...
我知道这只是弄清楚元素路线的问题,所以我不会完全没有希望...
System.Xml.XPathNodeList
My XML looks like this: 我的XML如下所示:
<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>nosignal</name>
<SSIDConfig>
<SSID>
<hex>6E6F7369676E616C</hex>
<name>nosignal</name>
</SSID>
<nonBroadcast>true</nonBroadcast>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<autoSwitch>false</autoSwitch>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
<FIPSMode xmlns="http://www.microsoft.com/networking/WLAN/profile/v2">false</FIPSMode>
</authEncryption>
<sharedKey>
<keyType>passPhrase</keyType>
<protected>true</protected>
<keyMaterial>01000000D</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>
[ EDIT with the help of LB the new code looks like this and it WORKS! [在LB的帮助下进行编辑,新代码看起来像这样,它起作用了! ] [ For anyone that is struggling with a similar problem.
] [对于正在遇到类似问题的任何人。 ]
]
My Class is: 我的课程是:
class ProfileManager
{
public static string readProfile() {
XmlDocument wifiProfile = new XmlDocument();
string path = @"C:\temp\nosignal.xml";
string password = "";
wifiProfile.Load(path);
XmlNamespaceManager mgr = new XmlNamespaceManager(wifiProfile.NameTable);
mgr.AddNamespace("ns", "http://www.microsoft.com/networking/WLAN/profile/v1");
XmlNodeList sharedKeyNodes = wifiProfile.SelectNodes("//ns:WLANProfile/ns:MSM/ns:security/ns:sharedKey", mgr);
foreach (XmlNode itemNode in sharedKeyNodes)
{
XmlNode keyMaterialNode = itemNode.SelectSingleNode("ns:keyMaterial", mgr);
if (keyMaterialNode != null)
{
password = keyMaterialNode.InnerText;
}
}
return password;
}
}
I'm close, but still just a bit stuck. 我很近,但仍然有点卡住。 Any help would be appreciated!!!
任何帮助,将不胜感激!!! Thank you!
谢谢!
You don't use the default XmlNamespace " http://www.microsoft.com/networking/WLAN/profile/v1 " 您不使用默认的XmlNamespace“ http://www.microsoft.com/networking/WLAN/profile/v1 ”
wifiProfile.Load(path);
XmlNamespaceManager mgr = new XmlNamespaceManager(wifiProfile.NameTable);
mgr.AddNamespace("ns", "http://www.microsoft.com/networking/WLAN/profile/v1");
XmlNodeList sharedKeyNodes = wifiProfile.SelectNodes("//ns:WLANProfile/ns:MSM/ns:security/ns:sharedKey",mgr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.