如何从下面的 xml 中检索 sitename 元素值?
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:63630/Service.svc</To>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/GetDemographic</Action>
</s:Header>
<s:Body>
<GetDemographic xmlns="http://tempuri.org/">
<userIdentifier>first value</userIdentifier>
<sitename>second value</sitename>
<demographicName>third value</demographicName>
</GetDemographic>
</s:Body>
</s:Envelope>
我试过的以下代码返回了 null:
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(myXml);
var result = xmlDocument.SelectNodes("//sitename");
Xml 命名空间有问题吗?我是否可以不考虑 namespace 值进行搜索,因为站点名称元素没有分配给它的 namespace ?
我发现下面的代码工作正常:
xmlDocument.SelectNodes("//*[local-name()='sitename']");
如何让它不区分大小写?
最佳答案
尝试查看那些链接 ;)
.NET 中不区分大小写的 XPath:http://blogs.msdn.com/shjin/archive/2005/07/22/442025.aspx
使用 XPath 对 MSXML 执行不区分大小写的搜索:http://support.microsoft.com/kb/315719
例如:
XMLDoc.SelectNodes("Cars/car[contains(.,'Protan')]")
上面select的结果应该和这个select的结果相等
XMLDoc.SelectNodes("Cars/car[contains(.,'protan')]")'
关于c# - 如何使用 xpath 从 xml 中检索元素值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15639617/