Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Uwp 获取csproj 所有文件,并获取文件生成选项. Uwp Get all the files in the *.csproj.
/// <summary>
/// 获取csproj 所有文件,并获取文件生成选项
/// Get all the files in the *.csproj
/// </summary>
/// <param name="doc">csproj 文件的XDocument</param>
/// <returns>
/// 文件路径和文件生成选项列表
/// The List that include the file path and it's compile options
/// </returns>
public static List<PrjFile> GetPrjFile(XDocument doc)
{
var projectXml = doc.Root;
var name = projectXml.Attribute("xmlns")?.Value;//NameSpace
//如果只写 projectXml.Elements("ItemGroup"),结果null
//It return null when use projectXml.Elements("ItemGroup") rather than use projectXml.Elements(XName.Get("ItemGroup", name))
//foreach (var file in projectXml.Elements(XName.Get("ItemGroup", name)))
//{
// 获取所有的项
// foreach (var temp in file.Elements())
// {
// prjFile.Add(new PrjFile()
// {
// File = temp.Attribute(XName.Get("Include")).Value,
// Form = temp.Name.LocalName
// });
// }
//}
return (from file in projectXml.Elements(XName.Get("ItemGroup", name))
from temp in file.Elements()
select new PrjFile()
{
File = temp.Attribute(XName.Get("Include")).Value,
Form = temp.Name.LocalName
}).ToList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment