2016-06-29 6 views
0

Ich habe eine Soap-Hülle, die als Anfragepaket für einen Webservice verwendet wird. Ich setze die Knotenwerte dynamisch unter Verwendung der Werte von einem Gitter xDoc.SelectSingleNode (xPath, oManager) .InnerXml = r.Cells [2] .Value.ToString();C# - xmldoc.selectSingleNode (xpath, nsmanager) gibt null zurück

Mein xml Paket ist:

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

Wenn ich versuche Wert Userpassword Knoten zu setzen, selectSingleNode null zurückkehrt. Mein Namespacemanger-Code ist wie folgt.

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; 
     } 

Muss ich meinen xpath explizit ändern, um Präfixe zu meinem Standardnamespace hinzuzufügen? Gibt es einen Ansatz, um dies dynamisch zu tun - Hinzufügen von Präfixen zu jedem Knoten mit dem Namespacemanager? Bitte helfen ..

Antwort

0

Ich habe eine Abhilfe für dieses Problem gefunden, ohne irgendwelche Manipulationen im Xpath. Hier geht meine Lösung.

Ersetzen Sie den Standard-Namespace ‚xmlns =‘ mit ‚xns =‘ (oder jede andere Token die nicht einen möglichen Konflikt mit bestehenden Präfixe sein) mit xDoc .Nach gesamte Verarbeitung erfolgt, bevor Sie Service zurückschicken Aktualisieren Sie es in seinen vorherigen Zustand, um alle Namespace Probleme von dem Dienst zu vermeiden.

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