Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

Linq to XML, Failing to get node values

I have following piece of XML that I receive from a web service call.

<Wireless xmlns="http://www.hikvision.com/ver10/XMLSchema">
  <enabled>true</enabled>
  <ssid>Network 1</ssid>
  <WirelessSecurity>
    <securityMode>WPA-personal</securityMode>
  </WirelessSecurity>
  <WPA>
    <algorithmType>TKIP</algorithmType>
    <sharedKey>asdfasdf</sharedKey>
    <wpaKeyLength>13</wpaKeyLength>
  </WPA>
  <WEP>
    <authenticationType>auto</authenticationType>
    <defaultTransmitKeyIndex>224</defaultTransmitKeyIndex>
    <EncryptionKeyList>
      <encryptionKey>AScii</encryptionKey>
    </EncryptionKeyList>
  </WEP>
</Wireless>

I want to go through it and populate an object but I am failing to perform the most basic operations. I have successfully parsed it in an XDocument named document. When I write the statement

var elem = document.Element("Wireless");// returns null
var elem = document.Element("ssid");// also returns null
var elem = document.Descendants("ssid");//produces empty enumeration

What I am doing wrong here? Is there a problem due to a different xml namespace?

Upvotes: 1

Views: 81

Answers (1)

L.B
L.B

Reputation: 116168

Yes the problem is related to xml namespace.

Declare a namespace as

XNamespace ns = "http://www.hikvision.com/ver10/XMLSchema";

and get the elements by

 var elem = document.Element(ns + "Wireless");
 var elems = document.Descendants(ns + "ssid");

Upvotes: 2

Related Questions