使用正则表达式去除多余注释及冗余数字位,关键代码:
string pattern = @"/b(/d+)/.(/d{1})/d+([/,/s]?)/b";
string temp = Regex.Replace(oldContent, pattern, @"$1.$2$3");
temp = Regex.Replace(temp, @"<!--.*-->", "");
temp = Regex.Replace(temp, @"[/s| ]*/r","");
运行效果截图:
// ReplaceWindow.xaml
<Window x:Class="BrawDraw.Com.AIXamlConverter.ReplaceWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="800">
<Grid>
<Label Height="28" Margin="45,17,219,0" Name="label1" VerticalAlignment="Top" Foreground="Red">将XAML内容复制到下面文本框内,然后点替换,在“结果”文本框中得到新内容。</Label>
<Button Height="23" HorizontalAlignment="Right" Margin="0,17,119,0" Name="btnReplace" VerticalAlignment="Top" Width="75" Click="btnReplace_Click">替换</Button>
<TextBox Margin="124,45,26,0" Name="txtBoxOldContent" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" Height="151" VerticalAlignment="Top" />
<TextBox Margin="124,200,26,201" Name="txtBoxResultContent" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" />
<Label Height="28" HorizontalAlignment="Left" Margin="45,45,0,0" Name="label2" VerticalAlignment="Top" Width="91">替换前内容:</Label>
<Label Height="28" HorizontalAlignment="Left" Margin="45,200,0,0" Name="label3" VerticalAlignment="Top" Width="91">结果:</Label>
<StackPanel Margin="123,0,27,12" Height="180" VerticalAlignment="Bottom">
<Border BorderBrush="Black" BorderThickness="1" Height="180" Name="borderResult">
<StackPanel Name="spResult" />
</Border>
</StackPanel>
<Label Height="28" HorizontalAlignment="Left" Margin="45,0,0,164" Name="label4" VerticalAlignment="Bottom" Width="91">效果预览:</Label>
</Grid>
</Window>
// ReplaceWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Text.RegularExpressions;
namespace BrawDraw.Com.AIXamlConverter
{
/// <summary>
/// Interaction logic for ReplaceWindow.xaml
/// </summary>
public partial class ReplaceWindow : Window
{
public ReplaceWindow()
{
InitializeComponent();
}
private void btnReplace_Click(object sender, RoutedEventArgs e)
{
string oldContent = txtBoxOldContent.Text;
//string pattern = @"(/d+)/.(/d)/d{5,}([/,/s]?)";
string pattern = @"/b(/d+)/.(/d{1})/d+([/,/s]?)/b";
string temp = Regex.Replace(oldContent, pattern, @"$1.$2$3");
temp = Regex.Replace(temp, @"<!--.*-->", "");
temp = Regex.Replace(temp, @"[/s| ]*/r","");
txtBoxResultContent.Text = temp;
try
{
//下面的代码用于预览效果显示:
System.IO.StringReader stringReader = new System.IO.StringReader(temp);
System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
UIElement uie = (UIElement)System.Windows.Markup.XamlReader.Load(xmlReader);
spResult.Children.Add(uie);
borderResult.BorderBrush = Brushes.Transparent;
}
catch (Exception exc)
{
MessageBox.Show(string.Format("出错了,错误是:/r/n{0}", exc.Message));
}
}
}
}