从 csproj 文件中读取引用列表 [英] Reading the list of References from csproj files

查看:34
本文介绍了从 csproj 文件中读取引用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道以编程方式读取 VS2008 csproj 文件中的引用列表的方法?MSBuild 似乎不支持此功能.我试图通过将 csproj 文件加载到 XmlDocument 中来读取节点,但是 XPath 搜索不返回任何节点.我正在使用以下代码:

Does anyone know of a way to programmatically read the list of References in a VS2008 csproj file? MSBuild does not appear to support this functionality. I'm trying to read the nodes by loading the csproj file into an XmlDocument but, the XPath search does not return any nodes. I'm using the following code:

System.Xml.XmlDocument projDefinition = new System.Xml.XmlDocument();
        projDefinition.Load(fullProjectPath);

        System.Xml.XPath.XPathNavigator navigator = projDefinition.CreateNavigator();

        System.Xml.XPath.XPathNodeIterator iterator = navigator.Select(@"/Project/ItemGroup");
        while (iterator.MoveNext())
        {
            Console.WriteLine(iterator.Current.Name);
        }

如果我能得到 ItemGroups 的列表,我就可以确定它是否包含 Reference 信息.

If I can get the list of ItemGroups I can determine whether it contains Reference information or not.

推荐答案

XPath 应该是 /Project/ItemGroup/Reference,而你忘记了命名空间.我只是使用 XLINQ - 在 XPathNavigator 中处理名称空间相当混乱.所以:

The XPath should be /Project/ItemGroup/Reference, and you have forgot the namespace. I'd just use XLINQ - dealing with namespaces in XPathNavigator is rather messy. So:

    XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
    XDocument projDefinition = XDocument.Load(fullProjectPath);
    IEnumerable<string> references = projDefinition
        .Element(msbuild + "Project")
        .Elements(msbuild + "ItemGroup")
        .Elements(msbuild + "Reference")
        .Select(refElem => refElem.Value);
    foreach (string reference in references)
    {
        Console.WriteLine(reference);
    }

这篇关于从 csproj 文件中读取引用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆