Xpath для элемента с двоеточием в имени элемента - PullRequest
0 голосов
/ 13 июня 2011

мой 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)

Ответы [ 3 ]

3 голосов
/ 13 июня 2011

Ваше выражение XPath должно содержать пространство имен при выборе узла с примененным пространством имен.[Ссылка]

Таким образом, выражения XPath должны быть следующими

//fed:TargetScope/EndpointReference/Address

вместо

//TargetScope/EndpointReference/Address
0 голосов
/ 13 июня 2011

В дополнение к

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 .

0 голосов
/ 13 июня 2011

Может быть, это поможет ... попробуйте код ниже:

foreach (XmlNode targetScopeNode in fedMetaDocument.GetElementsByTagName("Address"))
{
  targetScopeNode.InnerText = tsakListUrl;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...