C# 元素名称中带有冒号的元素的Xpath

C# 元素名称中带有冒号的元素的Xpath,c#,xml,xpath,C#,Xml,Xpath,我的xml是 <?xml version="1.0" encoding="utf-8"?> <EntityDescriptor ID="_2d6175bd-f939-49f2-a980-db4179f32074" entityID="https://server1.domain.com:xx3/yyy/" xmlns="urn:oasis:names:tc:SAML:2.0:metadata"> <RoleDescriptor xsi:type="fed:App

我的xml是

<?xml version="1.0" encoding="utf-8"?>
<EntityDescriptor ID="_2d6175bd-f939-49f2-a980-db4179f32074" entityID="https://server1.domain.com:xx3/yyy/" xmlns="urn:oasis:names:tc:SAML:2.0:metadata">
  <RoleDescriptor xsi:type="fed:ApplicationServiceType" xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" protocolSupportEnumeration="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <fed:ClaimTypesRequested>
      <auth:ClaimType Uri="http://schemas.microsoft.com/ws/2008/06/identity" Optional="true" xmlns:auth="http://docs.oasis-open.org/wsfed/authorization/200706" />
    </fed:ClaimTypesRequested>
    <fed:TargetScopes>
      <EndpointReference xmlns="http://www.w3.org/2005/08/addressing">
        <Address>https://baarnntl1/</Address>
      </EndpointReference>
    </fed:TargetScopes>
    <fed:PassiveRequestorEndpoint>
      <EndpointReference xmlns="http://www.w3.org/2005/08/addressing">
        <Address>https://baarnntl1/</Address>
      </EndpointReference>
    </fed:PassiveRequestorEndpoint>
  </RoleDescriptor>
</EntityDescriptor>
我犯了一个错误

System.Xml.XPath.XPathException was unhandled by user code Message=Expression must evaluate to a node-set. Source=System.Xml StackTrace: at MS.Internal.Xml.XPath.XPathParser.ParseNodeTest(AstNode qyInput, AxisType axisType, XPathNodeType nodeType) at MS.Internal.Xml.XPath.XPathParser.ParseStep(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseRelativeLocationPath(AstNode qyInput) System.Xml.XPath.XPathException未由用户代码处理 Message=表达式必须计算为节点集。 Source=System.Xml 堆栈跟踪: 位于MS.Internal.Xml.XPath.XPathParser.ParseNodeTest(AstNode qyInput、AxisType、XPathNodeType、nodeType) 位于MS.Internal.Xml.XPath.XPathParser.ParseStep(AstNode qyInput) 位于MS.Internal.Xml.XPath.XPathParser.ParseRelativeLocationPath(AstNode qyInput)
选择应用了命名空间的节点时,XPath表达式应包含命名空间。[参考]

因此XPath表达式应该如下所示

//fed:TargetScope/EndpointReference/Address
而不是

//TargetScope/EndpointReference/Address

选择应用了命名空间的节点时,XPath表达式应包含命名空间。[参考]

因此XPath表达式应该如下所示

//fed:TargetScope/EndpointReference/Address
而不是

//TargetScope/EndpointReference/Address

也许这有帮助…请尝试下面的代码:

foreach (XmlNode targetScopeNode in fedMetaDocument.GetElementsByTagName("Address"))
{
  targetScopeNode.InnerText = tsakListUrl;
}

也许这有帮助…请尝试下面的代码:

foreach (XmlNode targetScopeNode in fedMetaDocument.GetElementsByTagName("Address"))
{
  targetScopeNode.InnerText = tsakListUrl;
}
除了

mgr.AddNamespace("fed", "http://docs.oasis-open.org/wsfed/federation/200706");
您需要为默认命名空间声明前缀:

mgr.AddNamespace("meta", "urn:oasis:names:tc:SAML:2.0:metadata");
fedMetaDocument.SelectNodes("fed:TargetScopes/meta:EndpointReference/meta:Address", mgr))
然后在该命名空间中的所有元素上使用它:

mgr.AddNamespace("meta", "urn:oasis:names:tc:SAML:2.0:metadata");
fedMetaDocument.SelectNodes("fed:TargetScopes/meta:EndpointReference/meta:Address", mgr))
如果您不了解基本原理,名称空间就是其中之一,如果您试图通过反复尝试让它们正常工作,它会让您真正受挫。除此之外,请参见。

mgr.AddNamespace("fed", "http://docs.oasis-open.org/wsfed/federation/200706");
您需要为默认命名空间声明前缀:

mgr.AddNamespace("meta", "urn:oasis:names:tc:SAML:2.0:metadata");
fedMetaDocument.SelectNodes("fed:TargetScopes/meta:EndpointReference/meta:Address", mgr))
然后在该命名空间中的所有元素上使用它:

mgr.AddNamespace("meta", "urn:oasis:names:tc:SAML:2.0:metadata");
fedMetaDocument.SelectNodes("fed:TargetScopes/meta:EndpointReference/meta:Address", mgr))

如果您不了解基本原理,名称空间就是其中之一,如果您试图通过反复尝试让它们正常工作,它会让您真正受挫。请参阅。

您的xml在我看来不像xml!我看不出问题中有XMLstatement@vickirk:应该标记mod注意吗?您的xml在我看来不像xml!我看不出问题中有XMLstatement@vickirk:是否应标记以引起mod注意?您还需要根据
xmlns:fed
将前缀
fed:
映射到相应的命名空间。他在
mgr.AddNamespace
中这样做,OP不仅需要fed命名空间的命名空间前缀,但是还需要为默认名称空间声明并使用前缀“urn:oasis:names:tc:SAML:2.0:metadata”。您还需要根据
xmlns:fed
将前缀
fed:
映射到适当的名称空间。他在
mgr.AddNamespace
中这样做,OP不仅需要fed名称空间的名称空间前缀,但是还需要为默认名称空间声明并使用前缀,“urn:oasis:names:tc:SAML:2.0:metadata”。