2016-06-29 36 views
0

我有一個Soap信封被用作web服務的請求數據包。 我使用網格中的值動態地設置節點值 xDoc.SelectSingleNode(xPath,oManager).InnerXml = r.Cells [2] .Value.ToString();C# - xmldoc.selectSingleNode(xpath,nsmanager)返回null

我的XML數據包:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> 
    <CardActivation xmlns="www.testclient.com"> 
     <requestData xmlns:d4p1="http://schemas.testdata.org/2004/07/ClientServices.DTO" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <LoginUser xmlns="http://schemas.testdata.org/2004/07/ClientServices">testuser</LoginUser> 
     <UserPassword xmlns="http://schemas.testdata.org/2004/07/ClientServices">Testped</UserPassword> 
     <IPAddress xmlns="http://schemas.testdata.org/2004/07/ClientServices">10.211.1.22</IPAddress> 
     <UniqueIDFlag xmlns="http://schemas.testdata.org/2004/07/ClientServices">0</UniqueIDFlag> 
     <UniqueID xmlns="http://schemas.testdata.org/2004/07/ClientServices">123456789</UniqueID> 
     <Source xmlns="http://schemas.testdata.org/2004/07/ClientServices">WS</Source> 
     <APIVersion xmlns="http://schemas.testdata.org/2004/07/ClientServices">1.2</APIVersion> 
     <ApplicationVersion xmlns="http://schemas.testdata.org/2004/07/ClientServices">1</ApplicationVersion> 
     <CallerID xmlns="http://schemas.testdata.org/2004/07/ClientServices">221221</CallerID> 
     <CalledID xmlns="http://schemas.testdata.org/2004/07/ClientServices">3333</CalledID> 
     <SessionID xmlns="http://schemas.testdata.org/2004/07/ClientServices">221111</SessionID> 
     <ANI i:nil="true" xmlns="http://schemas.testdata.org/2004/07/ClientServices" /> 
     <DNS i:nil="true" xmlns="http://schemas.testdata.org/2004/07/ClientServices" /> 
     <Language i:nil="true" xmlns="http://schemas.testdata.org/2004/07/ClientServices" /> 
     <RequestDate xmlns="http://schemas.testdata.org/2004/07/ClientServices">2014-10-08T00:00:00</RequestDate> 
     <d4p1:CardNumber i:nil="true" /> 
     <d4p1:ProxyNumber>1123</d4p1:ProxyNumber> 
     <d4p1:CardExpiryDate>2105</d4p1:CardExpiryDate> 
     </requestData> 
    </CardActivation> </s:Body> </s:Envelope> 

當我試圖值設置爲userPassword的節點,是的selectSingleNode返回null。我的命名空間代碼如下。

public static XmlNamespaceManager getAllNamespaces(XmlDocument xDoc) 
     { 
      XmlNamespaceManager result = new XmlNamespaceManager(xDoc.NameTable); 

      IDictionary<string, string> localNamespaces = null; 
      XPathNavigator xNav = xDoc.CreateNavigator(); 
      while (xNav.MoveToFollowing(XPathNodeType.Element)) 
      { 
       localNamespaces = xNav.GetNamespacesInScope(XmlNamespaceScope.Local); 
       // localNamespaces = xNav.GetNamespacesInScope(XmlNamespaceScope.All); 
       foreach (var localNamespace in localNamespaces) 
       { 
        string prefix = localNamespace.Key; 
        if (string.IsNullOrEmpty(prefix)) 
         prefix = "DEFAULT"; 

        result.AddNamespace(prefix, localNamespace.Value); 
       } 
      } 

      return result; 
     } 

是否需要顯式更改我的xpath以將前綴添加到我的默認命名空間?有沒有辦法做到這一點 - 使用namespacemanager爲每個節點添加前綴?請幫助..

回答

0

我找到了解決此問題的方法,而無需在xpath中執行任何操作。 這裏是我的解決方案。

與「XNS =」替換默認命名空間「的xmlns =」(或任何其它令牌 這不會是與任何現有的前綴的可能的衝突)。經過 整個處理與完成XDOC,發送給服務之前,請將其更新爲 ,以回到其之前的狀態,以避免與該服務相關的任何命名空間相關的 問題。

  if (xDoc.InnerXml.Contains("xmlns=\"")) 
       { 
        xDoc.InnerXml = xDoc.InnerXml.Replace("xmlns=\"", "xns=\""); 
       }