我有以下XML结构:
<?xml version="1.0" encoding="utf-16"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<StoreResponse xmlns="http://www.some-site.com">
<StoreResult>
<Message />
<Code>OK</Code>
</StoreResult>
</StoreResponse>
</soap:Body>
</soap:Envelope>
我需要从本文档中获取 Code
中的InnerText,我需要有关相应XPATH语句的帮助 .
我'm really confused by XML namespaces. While working on a previous namespace problem in another XML document, I learned, that even if there'在 Code
之前没有任何内容(例如 ns:Code
),它仍然是其父节点中xmlns属性定义的命名空间的一部分 . 现在,在 Code
的父项中定义了多个xmlns节点 . 我需要在XPATH语句中指定什么名称空间?是否有"primary namespace"这样的事情?子节点是否继承了父节点的(主)名称空间?
3 回答
<Code>元素的名称空间是http://www.some-site.com . xmlsn:xxx表示前缀为xxx的名称:(如soap:Body)具有该命名空间 . xmlns本身意味着这是没有任何前缀的名称的默认命名空间 .
使用XDocument(Linq)方法的示例:
后代元素将继承最后一个直接命名空间 . 在您的示例中,您将需要为soap信封创建两个名称空间,为“some-site”创建第二个名称空间 .
这是我在这个问题中找到的一个选项:Weirdness with XDocument, XPath and namespaces