XPath和*的.csproj [英] XPath and *.csproj
问题描述
我肯定在这里缺少一些重要的细节。我不能让与Visual Studio项目文件。NET的XPath的工作。
I am for sure missing some important detail here. I just cannot make .NET's XPath work with Visual Studio project files.
让我们加载XML文档:
Let's load an xml document:
var doc = new XmlDocument();
doc.Load("blah/blah.csproj");
现在执行我的查询:
var nodes = doc.SelectNodes("//ItemGroup");
Console.WriteLine(nodes.Count); // whoops, zero
当然,也有文件中命名的ItemGroup节点。此外,该查询的工作:
Of course, there are nodes named ItemGroup in the file. Moreover, this query works:
var nodes = doc.SelectNodes("//*/@Include");
Console.WriteLine(nodes.Count); // found some
与其他文档,XPath的工作就好了。
我绝对疑惑了。任何人都可以解释我是怎么回事?
With other documents, XPath works just fine. I am absolutely puzzled about that. Could anyone explain me what is going on?
推荐答案
您可能需要添加到该命名空间的引用 http://schemas.microsoft.com/developer/msbuild/2003
。
You probably need to add a reference to the namespace http://schemas.microsoft.com/developer/msbuild/2003
.
我有一个类似的问题,我写了一篇关于它的here.做这样的事情:
I had a similar problem, I wrote about it here. Do something like this:
XmlDocument xdDoc = new XmlDocument();
xdDoc.Load("blah/blah.csproj");
XmlNamespaceManager xnManager =
new XmlNamespaceManager(xdDoc.NameTable);
xnManager.AddNamespace("tu",
"http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode xnRoot = xdDoc.DocumentElement;
XmlNodeList xnlPages = xnRoot.SelectNodes("//tu:ItemGroup", xnManager);
这篇关于XPath和*的.csproj的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!