简体   繁体   English

从项目csproj删除节点

[英]delete node from project csproj

My project.csproj file contain below three compile include . 我的project.csproj文件包含以下三个编译include。 i wanted to delete all three compile include at once. 我想一次删除所有三个编译包含。

<Compile Include="Orkut\Cart\Cart.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Cart.resx"></DependentUpon>
 </Compile>

<Compile Include="Orkut\Cart\Cart.Designer.de-DE.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Cart.de-DE.resx"></DependentUpon>
 </Compile>

<Compile Include="Orkut\Cart\Cart.Designer.sv-SE.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Cart.sv-SE.resx"></DependentUpon>
 </Compile>

i have written code for this but it is deleting after checking the value 我已经为此编写了代码,但是在检查值之后将其删除

  string fileName=@"D:\projectpath\abc.csproj";
     XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";

        XDocument xDoc = XDocument.Load(fileName);

 var b1 = xDoc.Descendants(ns + "Compile")
            .Where(el => el.Attribute("Include").Value == @"Orkut\abc.Designer.cs");
if (b1 != null)
        {
            b1.Remove();

            xDoc.Save(fileName);
        }

Use IEnumerable<T>.Remove() extension to remove every matching node from its parent node: 使用IEnumerable<T>.Remove()扩展从其父节点中删除每个匹配的节点:

xDoc.Descendants(ns + "Compile")
    .Where(el => (string)el.Attribute("Include") == @"Orkut\abc.Designer.cs")
    .Remove();

xDoc.Save(fileName);

If you want to delete all Compile elements which has include containing Orkut\\Cart\\Cart.Designer then use following condition to select those elements: 如果要删除所有包含Orkut\\Cart\\Cart.Designer Compile元素,请使用以下条件选择这些元素:

Where(e => e.Attribute("Include").Value.StartsWith(@"Orkut\Cart\Cart.Designer"))

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

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