2011-06-13 53 views
0

我的XML是的XPath與元素結腸元素名稱

<?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) 
+0

XML上面缺失 – hari 2011-06-13 12:55:58

+0

您的xml看起來不像xml給我! – vickirk 2011-06-13 12:56:30

+0

我沒有看到問題陳述中的XML – 2011-06-13 12:59:37

回答

0

也許這可以幫助...嘗試下面的代碼:

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

感謝Reniuz,你的建議解決了我的問題 – hari 2011-06-14 06:58:14

2

你的XPath表達式在選擇應用了名稱空間的節點時應該包含名稱空間。 [參考價格]

所以XPath表達式應該是以下

//fed:TargetScope/EndpointReference/Address 

代替

//TargetScope/EndpointReference/Address 
+0

您還需要將'fed:'前綴映射到每個'xmlns:fed'的相應命名空間。 – user7116 2011-06-13 13:53:13

+1

他在'mgr.AddNamespace'中做到了這一點 – 2011-06-13 13:55:24

+0

我錯過了代碼牆+1。 – user7116 2011-06-13 13:55:50

0

另外到

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

命名空間是其中的一件事情是,如果你不瞭解基本原理,如果你試圖通過反覆試驗讓他們工作,真的會讓你失望。見this earlier answer of mine about the default namespace and XPath