[英]How to retrieve an element value from the xml using xpath?
How to retrieve the sitename element value from the below xml? 如何从下面的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>
The below code I tried returned null: 我尝试的以下代码返回null:
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(myXml);
var result = xmlDocument.SelectNodes("//sitename");
Is the problem the Xml Namespace? 是Xml命名空间的问题吗? Could I search regardless of the namespace values as sitename element has no namespace assigned to it?
是否可以搜索任何名称空间值,因为sitename元素没有分配名称空间?
I found the below code which works fine: 我发现以下代码可以正常工作:
xmlDocument.SelectNodes("//*[local-name()='sitename']");
How to make it case-insensitive? 如何使其不区分大小写?
Try to look those links ;) 尝试查找那些链接;)
Case-insensitive XPath in .NET: http://blogs.msdn.com/shjin/archive/2005/07/22/442025.aspx .NET中不区分大小写的XPath: http : //blogs.msdn.com/shjin/archive/2005/07/22/442025.aspx
Use XPath to Perform a Case-Insensitive Search with MSXML: http://support.microsoft.com/kb/315719 使用XPath通过MSXML执行不区分大小写的搜索: http : //support.microsoft.com/kb/315719
For example : 例如 :
XMLDoc.SelectNodes("Cars/car[contains(.,'Protan')]")
the result of above select should be equal with results of this select 以上选择的结果应与此选择的结果相等
XMLDoc.SelectNodes("Cars/car[contains(.,'protan')]")'
I had a similar problem. 我有一个类似的问题。 I was able to solve it by adding the namespace using a namespace manager.
我可以通过使用名称空间管理器添加名称空间来解决此问题。
Here's a similar Q/A: ( Using Xpath With Default Namespace in C# ) 这是一个类似的Q / A :( 在C#中将Xpath与默认命名空间一起使用 )
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.