0

私の project.csproj ファイルには、以下の 3 つの compile include が含まれています。3 つのコンパイル インクルードを一度にすべて削除したかったのです。

<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 に答える