gpt4 book ai didi

c# - 如何使用 xpath 从 xml 中检索元素值?

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:40 24 4
gpt4 key购买 nike

如何从下面的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com