我尝试从csproj文件中获取所有dll名称,但无法获得任何内容! 因此,我尝试使用liq查询从ItemGroup标记获取al元素:
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文件。我剪了一些不合时宜的文字))
<?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>
请帮助我获取这个名单!我做错了什么?
谢谢。
答案 0 :(得分:15)
你忽略了这一点:
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
为后代设置默认命名空间。所以你想要:
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);