简体   繁体   English

使用 XDocument 读取 csproj 时检索此 XElement 的更好方法是什么?

[英]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).我正在将文件添加到 vs.net 之外的 cs 项目(图像、css 等,我们组之外的文件,但对于发布是必需的)。 I'm loading the csproj and querying for the ItemGroup that contains the "Content" nodes.我正在加载 csproj 并查询包含“内容”节点的 ItemGroup。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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