开发者

What is a better way to retrieve this XElement when reading a csproj with XDocument?

开发者 https://www.devze.com 2023-03-09 01:48 出处:网络
I\'m adding files to the a cs project outside of vs.net (images, css, etc, files outside our group but are necessary to Publish). I\'m loading the csproj and querying for the ItemGroup tha开发者_开发问

I'm adding files to the a cs project outside of vs.net (images, css, etc, files outside our group but are necessary to Publish). I'm loading the csproj and querying for the ItemGroup tha开发者_开发问答t contains the "Content" nodes.

XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
XDocument projectDocument = XDocument.Load("someproject.csproj");

var itemGroup = projectDocument.Element(msbuild + "Project")
                               .Elements(msbuild + "ItemGroup")
                               .Descendants()
                               .Where(x => x.Name == msbuild +"Content")
                               .First().Parent;

Is there a better way to get this group?

Thank you.


You could do it like this:

var itemGroup = 
    projectDocument.Element(msbuild + "Project")
                   .Elements(msbuild + "ItemGroup")
                   .Where(x => x.Descendants()
                                .Any(y => y.Name == msbuild +"Content")
                         )
                   .FirstOrDefault();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号