开发者

Remove code snippet

I have a block of code like this:

<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>开发者_如何学Go;StatusInfo.aspx</DependentUpon>
</Compile>
<Compile Include="Share\System\Master\Status\StatusList.aspx.cs">
  <DependentUpon>StatusList.aspx</DependentUpon>
  <SubType>ASPXCodeBehind</SubType>
</Compile>

I want to remove all the block code that similar to:

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

I mean it begins with <Compile, contains .aspx.designer.cs and ends with </Compile>

So when I remove all the block code like that, all that remains will be something like this:

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

Because I converted a Website to a Web Application so I have to remove all the design.cs pages and recreate them so that error duplicated controls won't happen.

The thing is I have a lot of pages :((

Thanks in advance :)

Updated: I use Quick Find and Replace in Visual Studio


You will want to switch the regex matcher into single-line mode for this.

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

Generally, regular expressions are not a good choice for parsing XML. Its better to just load the XML document and filter/delete the nodes you want.

But, in this example, Regex can do what you want, because the structure of the document is relatively simple. It first looks for the Compile open tag, then finds the file name in the Include. It will match the file name until it finds a double-quote. It will then go back and ensure that the last characters it matched were aspx.designer.cs". It will then lazily look for the closing Compile tag, to keep it from gobbling the entire rest of the file.

EDIT: I don't think you can do this within the Visual Studio find/replace dialog. As far as I can tell, you cannot switch that to single-line mode. Whenever I try providing (?s) it starts just not matching anything. So there's no way to capture from the opening Compile tag to the closing Compile tag. It also seems that lookbehinds won't work either.

Why not use PowerShell? You can provide the above into PowerShell.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜