Accessing XElement having namespace inside a parent with different namespace

I am having an xelement like this.

<fiAPI xmlns="http://integration.fiapi.com" xmlns:ITI="http://www.ITIWnet.com/" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/xmlenc#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://integration.fiapi.com/fiAPI.xsd">
  <fiHeader Version="2.2">
    <Service Name="" Version="8.0">
      <DateTime>2015-04-23T16:09:39-05:00</DateTime>
      <UUID>d111bc1b-e539-47e6-93e0-d1c426346b78</UUID>
    </Service>
    <Security>
      <AuthenticationMaterial>
        <PrincipalPWD>
          <EncryptedData>
            <CipherData xmlns="http://www.w3.org/2001/04/xmlenc#">
              <CipherValue></CipherValue>
            </CipherData>
          </EncryptedData>
        </PrincipalPWD>
      </AuthenticationMaterial>
      <PrincipalID></PrincipalID>
      <TrustRelationship>User</TrustRelationship>
      <MessageDigest algorithm="SHA-1"></MessageDigest>
    </Security>
  </fiHeader>
</fiAPI>

i am trying to set some value to CipherValue tag like this.

var loginRequestNameSpace  = "http://integration.fiapi.com";
var cipherDataNameSpace = "http://www.w3.org/2001/04/xmlenc#";
XElement encryptedDataElement = loginRequestElement.Element(loginRequestNameSpace + "fiHeader").Element(loginRequestNameSpace + "Security").Element(loginRequestNameSpace + "AuthenticationMaterial").Element(loginRequestNameSpace + "PrincipalPWD").Element(loginRequestNameSpace + "EncryptedData");
XElement cypherDataElement = encryptedDataElement.Element(cipherDataNameSpace + "CipherData");

CipherData tag reads as NULL. I am doing something wrong with the namespace ?Can anybody help on this ?

Answers


You need to use a proper XName to correctly address the elements with different namespaces.

You can use the static method XName.Get("fiHeader", loginRequestNameSpace) for example instead of using string concatenation.


Need Your Help

In capistrano 3, is it possible to bundle & asset precompile locally and copy the results to application server?

ruby-on-rails-4 capistrano bundler capistrano3

At present, I have it setup so that capistrano git pulls the latest code on production servers, bundle installs and asset precompiles it individually on each web server.