使用SelectSingleNode()方法需要哪个命名空间(使用默认命名空间而不能使用该方法)
嗨,我有使用不同命名空间的xml文件(实际上是msbuild文件)
Value
但问题是因为我不能将SelectSingleNode与该文件一起使用
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
我相信它是因为默认命名空间(方法必需)因为上面的xmlns而消失了。 然后我想我只需要为此添加必要的……但我的尝试根本没有成功。 能否请您快速举例说明如何做到这一点?
我是这样做的。 (我也尝试添加多个名称空间,但没有成功..)
XmlDocument xml = new XmlDocument(); xml.Load("ref.props"); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003"); XmlNode platform_node = xml.SelectSingleNode("//msbld:PropertyGroup[contains(@Condition, '1111')]", nsmgr);
您需要使用正确的命名空间,即“ http://schemas.microsoft.com/developer/msbuild/2003
”。
尝试
XmlDocument xml = new XmlDocument(); xml.Load("ref.props"); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ms", "http://schemas.microsoft.com/developer/msbuild/2003"); XmlNode platform_node = xml.SelectSingleNode("/ms:Project/ms:PropertyGroup[contains(@Condition, '1111')]", nsmgr);
不要将名称空间前缀(在XML中为空)与命名空间混淆,后者是“ http://schemas.microsoft.com/developer/msbuild/2003
”。