它的命名空间是需要使用的SelectSingleNode()方法(使用默认命名空间,并且不能使用的方法) [英] Which namespace is necessary to use SelectSingleNode() method (using default namespace and can't use the method)
问题描述
您好我有一个使用不同的命名空间。
XML文件(实际上是的MSBuild文件)
<?XML版本= 1.0>?;
<项目ToolsVersion =4.0的xmlns =http://schemas.microsoft.com/developer/msbuild/2003>
<的PropertyGroup条件='$(键)'=='1111'>
<关键> VALUE< / Key与GT;
< /&的PropertyGroup GT;
< /项目>
但问题是我不能使用的SelectSingleNode用,因为
$ B该文件$ b
的xmlns =http://schemas.microsoft.com/developer/msbuild/2003
我相信这是因为默认命名空间(必需的方法),是因为上面的xmlns历程。然后,我想我只需要添加必要的一个用于..但我的尝试是不成功的。能否请你给我一个简单的例子是如何做到这一点?
下面是我怎么做。 (我也试图添加多个命名空间,但都没有成功。)
XmlDocument的XML =新的XmlDocument();
与XML.load(ref.props);
的XmlNamespaceManager nsmgr =新的XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace(msbld,http://schemas.microsoft.com/developer/msbuild/2003);
XmlNode的platform_node
= xml.SelectSingleNode(// msbld:的PropertyGroup [含有(@Condition,'1111')],nsmgr);
您需要使用的正确命名空间,它的是的 http://schemas.microsoft.com/developer/msbuild/2003
尝试
XmlDocument的XML =新的XmlDocument();
与XML.load(ref.props);
的XmlNamespaceManager nsmgr =新的XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace(MS,http://schemas.microsoft.com/developer/msbuild/2003);
XmlNode的platform_node
= xml.SelectSingleNode(/ MS:项目/ MS:的PropertyGroup [含有(@Condition,'1111')],
nsmgr);
不要混淆了命名空间,这是命名空间前缀(这是在XML空) http://schemas.microsoft.com/developer/msbuild/2003
Hi I have xml file (which is actually msbuild file) that uses different namespace
<?xml version="1.0" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(key)'=='1111'">
<Key>Value</Key>
</PropertyGroup>
</Project>
But the problem is I can't use SelectSingleNode with that file because of
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
I believe it's since default namespace (necessary for the method) is gone because of xmlns above. Then I think I just need to add necessary one for that.. But my tries weren't successful at all. Could you please give me a quick example how to do this?
Here is how I did. (I also tried to added multiple namespaces but weren't successful..)
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);
You need to use the correct namespace, which is "http://schemas.microsoft.com/developer/msbuild/2003
".
Try
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);
Don't confuse the namespace prefix (which was empty in the XML) with the namespace, which is "http://schemas.microsoft.com/developer/msbuild/2003
".
这篇关于它的命名空间是需要使用的SelectSingleNode()方法(使用默认命名空间,并且不能使用的方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!