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

发布于 2024-11-15 10:39:42 字数 2362 浏览 0 评论 0原文

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

我想更改地址元素值

XmlDocument fedMetaDocument = new XmlDocument();
fedMetaDocument.Load(federatedMetadataFile);
XmlNamespaceManager mgr = new XmlNamespaceManager(fedMetaDocument.NameTable);
mgr.AddNamespace("fed", "http://docs.oasis-open.org/wsfed/federation/200706");

foreach (XmlNode targetScopeNode in fedMetaDocument.SelectNodes("TargetScopes/EndpointReference/Address", mgr))
{
    targetScopeNode.Value = tsakListUrl;
}
foreach (XmlNode PassiveRequestorEndpointNode in fedMetaDocument.SelectNodes("TargetScopes/EndpointReference/Address", mgr))
{
    PassiveRequestorEndpointNode.Value = tsakListUrl;
}

我收到错误

  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)

my xml is

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

I want to change the address element value

XmlDocument fedMetaDocument = new XmlDocument();
fedMetaDocument.Load(federatedMetadataFile);
XmlNamespaceManager mgr = new XmlNamespaceManager(fedMetaDocument.NameTable);
mgr.AddNamespace("fed", "http://docs.oasis-open.org/wsfed/federation/200706");

foreach (XmlNode targetScopeNode in fedMetaDocument.SelectNodes("TargetScopes/EndpointReference/Address", mgr))
{
    targetScopeNode.Value = tsakListUrl;
}
foreach (XmlNode PassiveRequestorEndpointNode in fedMetaDocument.SelectNodes("TargetScopes/EndpointReference/Address", mgr))
{
    PassiveRequestorEndpointNode.Value = tsakListUrl;
}

I am getting an error

  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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清浅ˋ旧时光 2024-11-22 10:39:42

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

所以 XPath 表达式应该是以下

//fed:TargetScope/EndpointReference/Address

而不是

//TargetScope/EndpointReference/Address

Your XPath expression should contain the namespace when selecting a node with a namespace applied. [Reference]

So the XPath expressions should be the following

//fed:TargetScope/EndpointReference/Address

instead of

//TargetScope/EndpointReference/Address
追我者格杀勿论 2024-11-22 10:39:42

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

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

Maybe this helps...try code below:

foreach (XmlNode targetScopeNode in fedMetaDocument.GetElementsByTagName("Address"))
{
  targetScopeNode.InnerText = tsakListUrl;
}
风月客 2024-11-22 10:39:42

此外,

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

名称空间是那些如果您不了解基础知识的事物之一,如果您不了解基本原理,那么它真的会让您陷入困境你试图通过反复试验让它们工作。请参阅我之前关于默认命名空间和 XPath 的回答。

In addition to

mgr.AddNamespace("fed", "http://docs.oasis-open.org/wsfed/federation/200706");

you need to declare a prefix for the default namespace:

mgr.AddNamespace("meta", "urn:oasis:names:tc:SAML:2.0:metadata");

And then use it on all elements that are in that namespace:

fedMetaDocument.SelectNodes("fed:TargetScopes/meta:EndpointReference/meta:Address", mgr))

Namespaces are one of those things that, if you don't understand the fundamentals, will really trip you up if you try to just get them to work by trial and error. See this earlier answer of mine about the default namespace and XPath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文