【问题标题】:Reading an XML document with XMLDocument C# 'password' string not returning使用 XMLDocument C# 'password' 字符串读取 XML 文档不返回
【发布时间】:2014-07-27 22:31:26
【问题描述】:

我正在尝试阅读<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;

    }

}

我已经接近了,但还是有点卡住了。任何帮助,将不胜感激!!!谢谢!

【问题讨论】:

    标签: c# xml xmldocument


    【解决方案1】:

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

    【讨论】:

    • 我添加了这个,但它仍然没有返回任何内容。不过还是谢谢你的回答!
    • @ozfive it still isn't returning anything?我在发布之前对其进行了测试,它返回 1 结果.....
    • 我现在正在调试它,没有得到密码字符串的返回值
    • @ozfive 我很确定你没有使用 keyMaterial 的命名空间 :) XmlNode keyMaterialNode = itemNode.SelectSingleNode("ns:keyMaterial",mgr); 我以为你明白了......
    猜你喜欢
    • 2022-01-15
    • 2016-02-14
    • 1970-01-01
    • 2013-08-21
    • 2013-08-28
    • 2015-05-18
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多