解析与succint XML语法 [英] Parse XML with succint syntax
问题描述
在我的申请,我从一个Web服务转换为另一种。我得到一个XML响应为<code>的XmlDocument 。我试图让文档中的特定节点。我知道只有将永远是我要找的节点的一个实例。以前的实施者能够准确地得到他想要什么:
In my application, I'm converting from one web service to another. I get an XML response as an XmlDocument
. I'm trying to get specific nodes in the document. I know there will only ever be one instance of the node I'm looking for. The previous implementer was able to get exactly what he wants with:
XmlNode node = xmlDoc.SelectSingleNode("//result/geometry/location/lat/text()");
我试图做同样的我的回应,但总是空回来。我知道(依稀)他有什么XML响应样子,知道我的。但我不能用自己的语法。我得到空
不管是什么。我使用的是更复杂的语句:
I'm trying to do the same with my response, but always get null back. I know (vaguely) what his XML response looked like, and know mine. But I can't use his syntax. I get null
no matter what. I'm using a more complex statement:
XmlNode xmlNode = xmlDoc.GetElementsByTagName("StatusDescription").Item(0);
但是,正如你可以看到,它的丑陋。和 - 更糟 - 每当我尝试去多个节点深,我得到空回:
But, as you can see, it's ugly. And--worse--whenever I try to go more than one node deep, I get null back:
XmlNode xmlNode = xmlDoc.GetElementsByTagName("/ResourceSets/ResourceSet").Item(0);
我试着插入并在几个地方去掉斜线,但无济于事。以前的实施者回来的XML不是什么特别的事情;它只是XML。但他能跳遍轻松的地方。
I've tried inserting and removing slashes in several places, but to no avail. The XML the previous implementer got back isn't anything special; it's just XML. But he can jump all over the place with ease.
下面是他的XML响应的一个片段:
Here's a snippet of his XML response:
<GeocodeResponse>
<status>OK</status>
<result>
<geometry>
<location>
<lat>37.4217550</lat>
<lng>-122.0846330</lng>
</location>
</geometry>
</result>
</GeocodeResponse>
下面是我的一个片段:
<Response xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<StatusCode>200</StatusCode>
<StatusDescription>OK</StatusDescription>
<ResourceSets>
<ResourceSet>
<EstimatedTotal>1</EstimatedTotal>
<Resources>
<Location>
...
</Location>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>
任何想法,我怎么能遍历XML那么容易,因为他的?
Any idea how I can traverse the XML as easy as him?
推荐答案
这里的根本区别在于的他的XML没有默认命名空间,而的你的有默认这里的命名空间声明:
The fundamental difference here is that his XML doesn't have default namespace, while yours has default namespace declared here :
xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1"
顺便说一句,这个话题(针对XML默认命名空间的XPath查询)已经被问了很多先前以各种形式在这里SO。请注意,没有前缀和不带不同的默认命名空间声明,其中一个默认命名空间声明的元素及其所有后代都被认为是在前面提到的默认命名空间。和在一个命名空间来查询一个元件的一种可能的方式是通过使用一个空间管理器的前缀映射到命名空间URI,然后在XPath正确使用映射前缀,例如:
By the way, this topic (XPath query against XML with default namespace) has been asked a lot previously in various forms here in SO. Notice that the element where a default namespace is declared and all of its descendants without a prefix and without a different default namespace declaration are considered to be in that aforementioned default namespace. And one possible way to query an element in a namespace is by mapping a prefix to the namespace uri using a namespace manager, and then using the mapped prefix properly in the XPath, for example :
var nsManager As New XmlNamespaceManager(new NameTable());
nsManager.AddNamespace("d", "http://schemas.microsoft.com/search/local/ws/rest/v1");
XmlNode xmlNode = xmlDoc.SelectSingleNode("//d:StatusDescription", nsManager);
这篇关于解析与succint XML语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!