简体   繁体   中英

C# - xmldoc.selectSingleNode(xpath, nsmanager) returns null

I have a Soap envelope which is being used as request packet for a webservice. I am setting the nodes values dynamically using the values from a grid xDoc.SelectSingleNode(xPath, oManager).InnerXml = r.Cells[2].Value.ToString();

My xml packet is :

<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">1234567890123457502</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>

When I am trying to set value to UserPassword node, selectSingleNode is returning null. My namespacemanger code is as follows.

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

Do I need to explicitly change my xpath to add prefixes to my default namespace? Is there any approach to do this dynamically -adding prefixes to each node using the namespacemanager? Please help..

I found a workaround for this issue without doing any manipulations in the xpath. Here goes my solution.

Replace the default namespace 'xmlns= ' with 'xns='(or any other token which wont be a possible conflict with any existing prefixes).After entire processing is done with xDoc, before sending to service, update it back to its previous state for avoiding any namespace related issues from the service.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM