[英]How to retrieve an element value from the xml using xpath?
如何從下面的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命名空間的問題嗎? 是否可以搜索任何名稱空間值,因為sitename元素沒有分配名稱空間?
我發現以下代碼可以正常工作:
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')]")
以上選擇的結果應與此選擇的結果相等
XMLDoc.SelectNodes("Cars/car[contains(.,'protan')]")'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.