[英]SelectSingleNode always returns null?
Taking this simplifed example of my XML: 以这个简化的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>
I am trying to select the 'error' node using SelectSingleNode method, however using the following code it always returns NULL? 我试图使用SelectSingleNode方法选择'错误'节点,但是使用以下代码它总是返回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? 有什么建议?
You're missing the XML namespace defined by the <message>
node in your SelectSingleNode
call. 您缺少SelectSingleNode
调用中<message>
节点定义的XML命名空间。 Assuming oss
is an XmlDocument
instance, you need to do this: 假设oss
是一个XmlDocument
实例,您需要这样做:
XmlNamespaceManager nsMgr = new XmlNamespaceManager(oss.NameTable);
nsMgr.AddNamespace("ns", "http://www.mydomain.com/MyDataFeed");
XmlNode errorNode = oss.SelectSingleNode("/ns:message/ns:error", nsMgr);
Marc 渣
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.