使用XMLDocument读取XML文档C#'密码'字符串没有返回

时间:2014-07-27 22:31:26

标签: c# xml xmldocument

我正在尝试阅读<keyMaterial></keyMaterial>

之间的文字

我尝试使用//WLANProfile/MSM/security/sharedKey作为元素路由,如下面的代码所示。它拒绝返回一个值。我已经完成了调试器并在行后的断点处XmlNodeList sharedKeyNodes = wifiProfile.SelectNodes("//WLANProfile/MSM/security/sharedKey");

SharedKeyNodes对象不返回计数。我知道这只是弄清楚元素路线的问题,所以我不会完全无望地来到这里......

System.Xml.XPathNodeList

我的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>

[在LB的帮助下编辑新代码看起来像这样,它的工作原理! ] [对于任何正在努力解决类似问题的人。 ]

我的班级是:

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;

    }

}

我很接近,但仍然有点卡住了。任何帮助,将不胜感激!!!谢谢!

1 个答案:

答案 0 :(得分:2)

您不使用默认的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);