【问题标题】:Accessing XElement having namespace inside a parent with different namespace访问具有不同命名空间的父级内的命名空间的 XElement
【发布时间】:2016-03-05 00:43:28
【问题描述】:

我有一个这样的 xelement。

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

我正在尝试像这样为 CipherValue 标签设置一些值。

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 标记读取为 NULL。我在命名空间上做错了什么?有人可以帮忙吗?

【问题讨论】:

    标签: c# xml xml-parsing linq-to-xml


    【解决方案1】:

    您需要使用正确的XName 来正确处理具有不同命名空间的元素。

    例如,您可以使用静态方法XName.Get("fiHeader", loginRequestNameSpace),而不是使用字符串连接。

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 2018-02-03
      • 2015-01-08
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      相关资源
      最近更新 更多