我肯定在这里错过了一些重要的细节。我只是无法使.NET的XPath与Visual Studio Project文件一起工作。

让我们加载XML文档:

var doc = new XmlDocument();
doc.Load("blah/blah.csproj");

现在执行我的查询:

var nodes = doc.SelectNodes("//ItemGroup");
Console.WriteLine(nodes.Count); // whoops, zero

当然,文件中有名为ItemGroup的节点。此外,此查询有效:

var nodes = doc.SelectNodes("//*/@Include");
Console.WriteLine(nodes.Count); // found some

使用其他文档,XPath的工作正常。我对此感到非常困惑。谁能解释我发生了什么?

有帮助吗?

解决方案

您可能需要添加对名称空间的参考 http://schemas.microsoft.com/developer/msbuild/2003.

我也有类似的问题,我写了 这里. 。做这样的事情:

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);

其他提示

查看根名称空间;您必须包括一个XML-Namespace Manager,并使用“ // x:itemGroup”之类的查询,其中“ x”是您对根名称空间的指定别名。并将经理传递到查询中。例如:

        XmlDocument doc = new XmlDocument();
        doc.Load("my.csproj");

        XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
        mgr.AddNamespace("foo", doc.DocumentElement.NamespaceURI);
        XmlNode firstCompile = doc.SelectSingleNode("//foo:Compile", mgr);

我在以下位置发布了一个Linq / XML版本:

http://granadacoder.wordpress.com/2012/10/11/how-to-find-references-in-ac-project-file-csproj-using-linq-xml/

但这是它的要旨。它可能不是100%完美的……但是它显示了这个想法。

我在此处发布代码,因为在搜索答案时,我发现了此(原始帖子)。然后我厌倦了搜索并写了自己的书。

            string fileName = @"C:\MyFolder\MyProjectFile.csproj";

            XDocument xDoc = XDocument.Load(fileName);

            XNamespace ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");

            //References "By DLL (file)"
            var list1 = from list in xDoc.Descendants(ns + "ItemGroup")
                        from item in list.Elements(ns + "Reference")
                        /* where item.Element(ns + "HintPath") != null */
                    select new
                       {
                           CsProjFileName = fileName,
                           ReferenceInclude = item.Attribute("Include").Value,
                           RefType = (item.Element(ns + "HintPath") == null) ? "CompiledDLLInGac" : "CompiledDLL",
                           HintPath = (item.Element(ns + "HintPath") == null) ? string.Empty : item.Element(ns + "HintPath").Value
                       };


            foreach (var v in list1)
            {
                Console.WriteLine(v.ToString());
            }


            //References "By Project"
            var list2 = from list in xDoc.Descendants(ns + "ItemGroup")
                        from item in list.Elements(ns + "ProjectReference")
                        where
                        item.Element(ns + "Project") != null
                        select new
                        {
                            CsProjFileName = fileName,
                            ReferenceInclude = item.Attribute("Include").Value,
                            RefType = "ProjectReference",
                            ProjectGuid = item.Element(ns + "Project").Value
                        };


            foreach (var v in list2)
            {
                Console.WriteLine(v.ToString());
            }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top