0

我的 project.csproj 文件包含以下三个 compile include 。我想一次删除所有三个编译包含。

<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>

我为此编写了代码,但在检查值后将其删除

  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);
        }
4

1 回答 1

1

使用IEnumerable<T>.Remove()扩展从其父节点中删除每个匹配的节点:

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

xDoc.Save(fileName);

如果要删除Compile包含包含的所有元素,请Orkut\Cart\Cart.Designer使用以下条件选择这些元素:

Where(e => e.Attribute("Include").Value.StartsWith(@"Orkut\Cart\Cart.Designer"))
于 2013-08-23T10:02:35.513 回答