处理具有相同前缀和多个uri的元素的XML / Soap信封中的selectSingleNode(Xpath)

时间:2016-08-09 12:26:08

标签: c# xml web-services soap namespaces

对于多个uri,我有一个具有相同前缀ns2的Soap数据包:http://schemas.datacontract.org/2004/07/TestServiceshttp://schemas.datacontract.org/2004/07/TestServices.DTO

当我尝试使用xDoc.SelectSingleNode(xPath,oManager).InnerXml在XDoc中设置值时,由于多个uri冲突,它给出了null。有没有办法动态地解决这个问题?我不能对xpath进行任何更改,因为它来自不同格式的不同服务。我的命名空间管理器代码如下:

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);
                foreach (var localNamespace in localNamespaces)
                {
                    string prefix = localNamespace.Key;
                    if (string.IsNullOrEmpty(prefix))
                        prefix = "DEFAULT";
                    result.AddNamespace(prefix, localNamespace.Value);
                }
            }

            return result;
        }

foreach (Telerik.WinControls.UI.RadTreeNode tn in treeView.SelectedNode.Nodes)
   {
      if (tn.Text == r.Cells[0].Value.ToString())
             {
        string xPath = tn.FullPath;
        xPath = "//" + xPath.Replace(@"\", "/").Replace(" ", "");
        if (!(r.Cells[2].Value.ToString().StartsWith("<") && r.Cells[2].Value.ToString().EndsWith(">")) && !(r.Cells[2].Value.ToString().StartsWith("[") && r.Cells[2].Value.ToString().EndsWith("]")))
        {                                                   
            xDoc.SelectSingleNode(xPath, oManager).InnerXml = r.Cells[2].Value.ToString();

            }
         }
   }

我的肥皂信封如下:

<s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/'>
  <s11:Body>
    <ns1:AccountCreation xmlns:ns1='www.test.com'>
      <ns1:Data>
        <ns2:LoginUser xmlns:ns2='http://schemas.datacontract.org/2004/07/TestServices'>?XXX?</ns2:LoginUser>
        <ns2:UserPassword xmlns:ns2='http://schemas.datacontract.org/2004/07/TestServices'>?XXX?</ns2:UserPassword>
        <ns2:IPAddress xmlns:ns2='http://schemas.datacontract.org/2004/07/TestServices'>?XXX?</ns2:IPAddress>
        <ns2:ProductID xmlns:ns2='http://schemas.datacontract.org/2004/07/TestServices.DTO'>?999?</ns2:ProductID>
        <ns2:BusinessAccountNumber xmlns:ns2='http://schemas.datacontract.org/2004/07/TestServices.DTO'>?999.99?</ns2:BusinessAccountNumber>
        <ns2:StoreName xmlns:ns2='http://schemas.datacontract.org/2004/07/TestServices.DTO'>?XXX?</ns2:StoreName>
        <ns2:Title xmlns:ns2='http://schemas.datacontract.org/2004/07/TestServices.DTO'>?XXX?</ns2:Title>
      </ns1:Data>
    </ns1:AccountCreation>
  </s11:Body>
</s11:Envelope>

0 个答案:

没有答案