[英]C# - xmldoc.selectSingleNode(xpath, nsmanager) returns null
我有一个Soap信封,它被用作Web服务的请求包。 我正在使用来自网格xDoc.SelectSingleNode(xPath,oManager).InnerXml = r.Cells [2] .Value.ToString();的值动态设置节点值。
我的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">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>
当我尝试将值设置为UserPassword节点时,selectSingleNode返回null。 我的namespacemanger代码如下。
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以将前缀添加到默认名称空间? 有没有什么方法可以动态地做到这一点-使用命名空间管理器向每个节点添加前缀? 请帮忙..
我没有在xpath中进行任何操作就找到了解决此问题的方法。 这是我的解决方案。
将默认名称空间'xmlns ='替换为'xns ='(或任何其他与现有前缀不存在冲突的令牌)。使用xDoc完成整个处理之后,在发送到服务之前,将其更新回其先前的状态用于避免服务中任何与名称空间相关的问题。
if (xDoc.InnerXml.Contains("xmlns=\""))
{
xDoc.InnerXml = xDoc.InnerXml.Replace("xmlns=\"", "xns=\"");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.