C# 从 csproj 文件中读取引用列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1191151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading the list of References from csproj files
提问by
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:
有谁知道以编程方式读取 VS2008 csproj 文件中的引用列表的方法?MSBuild 似乎不支持此功能。我试图通过将 csproj 文件加载到 XmlDocument 中来读取节点,但是 XPath 搜索不返回任何节点。我正在使用以下代码:
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);
}
If I can get the list of ItemGroups I can determine whether it contains Reference information or not.
如果我可以获得 ItemGroups 的列表,我可以确定它是否包含参考信息。
采纳答案by Pavel Minaev
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:
XPath 应该是/Project/ItemGroup/Reference
,而您忘记了命名空间。我只是使用 XLINQ - 处理名称空间XPathNavigator
相当混乱。所以:
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);
}
回答by Sudhanshu Mishra
Building on @Pavel Minaev's answer, this is what worked for me (notice the added .Attributes line to read the Include attribute)
基于@Pavel Minaev 的回答,这对我有用(注意添加的 .Attributes 行来读取 Include 属性)
XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition = XDocument.Load(@"D:\SomeProject.csproj");
IEnumerable<string> references = projDefinition
.Element(msbuild + "Project")
.Elements(msbuild + "ItemGroup")
.Elements(msbuild + "Reference")
.Attributes("Include") // This is where the reference is mentioned
.Select(refElem => refElem.Value);
foreach (string reference in references)
{
Console.WriteLine(reference);
}
回答by Amadeus Sánchez
Based on @PavelMinaev's answer, I also added the "HintPath" Element to the output. I write the string array "references" to a ".txt" file.
根据@PavelMinaev 的回答,我还在输出中添加了“HintPath”元素。我将字符串数组“references”写入“.txt”文件。
XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projDefinition = XDocument.Load(@"C:\DynamicsFieldsSite.csproj");
var references = projDefinition
.Element(msbuild + "Project")
.Elements(msbuild + "ItemGroup")
.Elements(msbuild + "Reference")
.Select(refElem => (refElem.Attribute("Include") == null ? "" : refElem.Attribute("Include").Value) + "\n" + (refElem.Element(msbuild + "HintPath") == null ? "" : refElem.Element(msbuild + "HintPath").Value) + "\n");
File.WriteAllLines(@"C:\References.txt", references);