读取带&不带命名空间标签的节点 [英] Reading nodes with&without namespace tag

查看:30
本文介绍了读取带&不带命名空间标签的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像这样阅读xml文档:

I need to read xml documents like this :

<wcs:CoverageOffering>
<wcs:description>Generated from GeoTIFF</wcs:description>
<wcs:name>ndh:ndh-cyclone-mortality-risks-distribution</wcs:name>
                            ....

但是在某些服务器中,xml 文档是在没有命名空间标记的情况下实现的:

But in some servers the xml document is implemented without namespace tag :

<CoverageOffering>
<description>Generated from GeoTIFF</description>
<name>ndh:ndh-cyclone-mortality-risks-distribution</name>
                         ....

我怎样才能有效地阅读两者?(我为每个节点写了 if else 语句来控制这种情况,但这似乎不是一个好方法)

How can I read both in an efficient way? (I wrote if else statements for each node to control this condition but it seems not a good way to do it)

推荐答案

使用 XmlDocument 并将 wcs 命名空间添加到 XmlNamespaceManager:

Use XmlDocument and add wcs namespace to XmlNamespaceManager:

var document = new XmlDocument();
document.Load(...);
var nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("wcs", "http://...your ns");

var nl = document.SelectNodes("your xpath", nsmgr);

这篇关于读取带&amp;amp;不带命名空间标签的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆