繁体   English   中英

XmlDocument.SelectSingleNode

[英]XmlDocument.SelectSingleNode

我有一个soap xml消息,需要从给定的soap xml中获取单个节点值

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://tews6/wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Body>
<testResult>
<Status version="6.0" >
<NNO>277982b4-2917a65f-13ffb8c0-b09751f</NNO>
</Status>
<ProfileTab>
<Email>abc@gmail.com</Email>
<Name>abc</Name>
</Profile>
</testResult></soapenv:Body></soapenv:Envelope>

我需要获取Email节点的值。 我使用下面的代码

 rootNode = "soapenv:Envelope/soapenv:Body/ProfileTab/Email";

 var nsmgr = new XmlNamespaceManager(document.NameTable);
 nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
 nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");

 node = document.SelectSingleNode(rootNode,nsmgr);

它返回null。

您可以使用以下内容。

var rootNode = "soapenv:Envelope/soapenv:Body/tews6:testResult/tews6:ProfileTab/tews6:Email";

var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("tews6", "http://tews6/wsdl");
nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");

var node = doc.SelectSingleNode(rootNode, nsmgr);

尝试这个:

string xml="xml"; 
XDocument doc = XDocument.Parse(xml);
 XNamespace bodyNameSpace ="http://schemas.xmlsoap.org/soap/envelope/";
 var bodyXml = from _e in doc.Descendants(bodyNameSpace + "Body")
                          select _e;
 if (bodyXml.Elements().Count() == 0)
            {
                return;
            }
var email = from _e in bodyXml.First()Descendants("Email")
                                      select _e;
if(email.Count()==1)
{
    string emailAddress=email.First().Value;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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