2011-03-21 71 views
0

我有這樣的代碼塊:刪除代碼片斷

<Compile Include="Share\System\Master\Relative\RelativeList.aspx.designer.cs"> 
    <DependentUpon>RelativeList.aspx</DependentUpon> 
</Compile> 
<Compile Include="Share\System\Master\Status\StatusInfo.aspx.cs"> 
    <DependentUpon>StatusInfo.aspx</DependentUpon> 
    <SubType>ASPXCodeBehind</SubType> 
</Compile> 
<Compile Include="Share\System\Master\Status\StatusInfo.aspx.designer.cs"> 
    <DependentUpon>StatusInfo.aspx</DependentUpon> 
</Compile> 
<Compile Include="Share\System\Master\Status\StatusList.aspx.cs"> 
    <DependentUpon>StatusList.aspx</DependentUpon> 
    <SubType>ASPXCodeBehind</SubType> 
</Compile> 

我想刪除所有的塊代碼類似於:

<Compile Include="Share\System\Master\Relative\RelativeList.aspx.designer.cs"> 
    <DependentUpon>RelativeList.aspx</DependentUpon> 
</Compile> 

我的意思是它開始與<Compile,包含.aspx.designer.cs並以</Compile>

結束所以,當我刪除所有這樣的代碼塊時,所有仍然會是這樣的:

<Compile Include="Share\System\Master\Status\StatusInfo.aspx.cs"> 
    <DependentUpon>StatusInfo.aspx</DependentUpon> 
    <SubType>ASPXCodeBehind</SubType> 
</Compile> 
<Compile Include="Share\System\Master\Status\StatusList.aspx.cs"> 
    <DependentUpon>StatusList.aspx</DependentUpon> 
    <SubType>ASPXCodeBehind</SubType> 
</Compile> 

因爲我轉換了一個網站,Web應用程序,所以我必須刪除所有design.cs頁面並重新創建它們,這樣的錯誤重複控件不會發生。

事情是我有很多的網頁:((

感謝提前:)

更新:我使用快速查找和Visual Studio中更換

回答

0

你會想切換正則表達式匹配成單行模式這一點。

Regex.Replace(
    input, 
    @"<Compile Include=""[^""]*""(?<=aspx.designer.cs"").*?</Compile>", 
    "", 
    RegexOptions.Singleline); 

一般情況下,正則表達式是不是一個好的選擇用於解析XML。最好只加載XML文檔並過濾/刪除所需的節點。

但是,在這個例子中,正則表達式可以做你想做的事情,因爲文檔的結構相對簡單。它首先查找Compile open標籤,然後在Include中找到文件名。它將匹配文件名直到找到雙引號。然後它會返回並確保它匹配的最後一個字符是aspx.designer.cs"。然後它會懶惰地尋找結束的編譯標記,以防止吞噬整個文件的其餘部分。

編輯:我不認爲你可以在Visual Studio查找/替換對話框中執行此操作。據我所知,你不能切換到單線模式。每當我嘗試提供(?s)它開始只是不匹配任何東西。所以沒有辦法從打開的Compile標籤捕獲到關閉的Compile標籤。這似乎也看起來後腦也不行。

爲什麼不使用PowerShell?您可以將上述內容提供給PowerShell。

[Regex]::Replace($(cat .\XMLFile1.xml), '<Compile Include="[^"]*"(?<=aspx\.designer\.cs").*?</Compile>', "", "Singleline"); 
+0

謝謝,但這不適用於我:(我已更新問題中的一些信息 – 2011-03-21 11:46:39