ozfive
ozfive

Reputation: 380

Reading an XML document with XMLDocument C# 'password' string not returning

I'm trying to read the text in between <keyMaterial></keyMaterial>

I tried using //WLANProfile/MSM/security/sharedKey as the element route, seen in the code below. 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");

the SharedKeyNodes object doesn't return a count for. 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 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! ] [ 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!

Upvotes: 1

Views: 439

Answers (1)

L.B
L.B

Reputation: 116178

You don't use the default 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);

Upvotes: 2

Related Questions