简体   繁体   中英

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

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 that 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM