读取带有命名空间的xml的节点内容

Q:

Taking this simplifed example of my XML:

复制代码
1 <?xml version="1.0"?> 
2 <message xmlns="http://www.mydomain.com/MyDataFeed" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mydomain.com/MyDataFeed https://secure.mydomain/MyDataFeed/myDataFeed.xsd" requestId="13898" status="1">
3  <error>Invalid Login</error> 
4 </message>
复制代码

I am trying to select the 'error' node using SelectSingleNode method, however using the following code it always returns NULL?

1 XmlNode errorNode = oss.SelectSingleNode("/message/error"); 
2 if (errorNode != null)      
3     Console.Writeline("There is an error"); 

From research I have done this is related to namespaces but I simply can't get anything to work. Any advice?

 

A:

You're missing the XML namespace defined by the <message> node in your SelectSingleNode call. Assuming oss is an XmlDocument instance, you need to do this:

1 XmlNamespaceManager nsMgr = new XmlNamespaceManager(oss.NameTable); 
2 nsMgr.AddNamespace("ns", "http://www.mydomain.com/MyDataFeed");  
3 XmlNode errorNode = oss.SelectSingleNode("/ns:message/ns:error", nsMgr); 

 

转载自:http://stackoverflow.com/questions/1766254/selectsinglenode-always-returns-null

转载于:https://www.cnblogs.com/dannyqiu/articles/3044716.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值