元素名称中带有冒号的元素的 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: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
选择应用了命名空间的节点时,您的 XPath 表达式应包含命名空间。 [参考]
所以 XPath 表达式应该是以下
而不是
Your XPath expression should contain the namespace when selecting a node with a namespace applied. [Reference]
So the XPath expressions should be the following
instead of
也许这有帮助...尝试下面的代码:
Maybe this helps...try code below:
此外,
您还需要为默认名称空间声明一个前缀:
然后在该名称空间中的所有元素上使用它:
名称空间是那些如果您不了解基础知识的事物之一,如果您不了解基本原理,那么它真的会让您陷入困境你试图通过反复试验让它们工作。请参阅我之前关于默认命名空间和 XPath 的回答。
In addition to
you need to declare a prefix for the default namespace:
And then use it on all elements that are in that namespace:
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.