本文介绍了总的SelectSingleNode返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我的XML这simplifed例如:

Taking this simplifed example of my XML:

<?xml version="1.0"?>
<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">
<error>Invalid Login</error>
</message>



我试图使用selectSingleNode方法来选择'错误'的节点,但是使用下面的代码,它总是返回NULL?

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

XmlNode errorNode = oss.SelectSingleNode("/message/error");
if (errorNode != null)
     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?

推荐答案

您错过通过定义的XML命名空间中的<消息> 的SelectSingleNode 呼叫节点。假设 OSS 的XmlDocument 例如,你需要做的:

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:

XmlNamespaceManager nsMgr = new XmlNamespaceManager(oss.NameTable);
nsMgr.AddNamespace("ns", "http://www.mydomain.com/MyDataFeed");

XmlNode errorNode = oss.SelectSingleNode("/ns:message/ns:error", nsMgr);



马克

Marc

这篇关于总的SelectSingleNode返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 18:07