阅读参考由的csproj文件列表 [英] Reading the list of References from csproj files

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

问题描述

有谁知道的一种方法以编程方式读取在VS2008参考文献列表的csproj文件?的MSBuild似乎不支持此功能。我试图通过加载的csproj文件到XmlDocument但是,在XPath的搜索不返回任何节点读取节点。我用下面的code:

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名单我能确定它是否包含一些参考信息,或没有。

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

推荐答案

中的XPath应 /项目/ ItemGroup /参考,你已经忘记了命名空间。我只是用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天全站免登陆