通过C#解析CSPROJ-无法解析ItemGroup [英] Parse csproj via c# - cannot parse ItemGroup
本文介绍了通过C#解析CSPROJ-无法解析ItemGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试从csproj文件中获取所有dll名称,但是什么也无法获取! 因此,我尝试使用liq查询从ItemGroup标记中获取所有元素:
I try to get all dll names from csproj file, but cannot get anything! So,i try to get al elements from ItemGroup tag with liq query:
var elem = doc.Descendants("Project").Where(t => t.Attribute("ToolsVersion")!=null)
.Elements("ItemGroup").Elements("Reference").Where(r => r.Attribute("Include") != null);
var attrs = elem.Attributes();
Console.WriteLine(attrs.Count());
foreach (var e in attrs)
{
Console.WriteLine(e);
}
这是我的xml csproj文件.我剪了一些不完整的文字))
And this is my xml csproj file. I cut some unusfull text))
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="MyProject1" />
<Reference Include="MyProject2" />
<Reference Include="MyProject3" />
</ItemGroup>
</Project>
请帮助我获取此名称列表!我做错了什么?
Please, help me to get this list of names! What i do wrong?
谢谢.
推荐答案
您忽略了这一点:
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
设置后代的默认名称空间.所以你想要:
That sets the default namespace for descendants. So you want:
XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
var elem = doc.Descendants(ns + "Project")
.Where(t => t.Attribute("ToolsVersion")!=null)
.Elements(ns + "ItemGroup")
.Elements(ns + "Reference")
.Where(r => r.Attribute("Include") != null);
这篇关于通过C#解析CSPROJ-无法解析ItemGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文