2013-10-07 60 views
3

我嘗試從csproj文件獲取所有dll名稱,但無法獲取任何內容! 所以,我試圖擺脫的ItemGroup標籤元素人與LIQ查詢:通過c#解析csproj - 無法解析ItemGroup

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。我切一些unusfull文本))

<?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

相關(但不是一個笨蛋)http://stackoverflow.com/q/3745029/492 –

回答

14

你忽略這一點:

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); 
+0

謝謝! .... – user2545071

+0

嗯,以前不知道。命名空間只是前綴給節點,而不是屬性? 有沒有這方面的完整參考? –