ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 写一个去除AI2XAML注释及多余数字位数的WPF窗体程序

写一个去除AI2XAML注释及多余数字位数的WPF窗体程序

原创 Linux操作系统 作者:大可山 时间:2009-01-06 10:36:54 0 删除 编辑

使用正则表达式去除多余注释及冗余数字位,关键代码:

            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","");

运行效果截图:
XamlExportWPFWindow

// ReplaceWindow.xaml

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="800">
   
       
       
       
       
       
       
       
       
                   
       

       

       
   



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


    /// Interaction logic for ReplaceWindow.xaml
    ///

    public partial class ReplaceWindow : Window
    {
        public ReplaceWindow()
        {
            InitializeComponent();
        }

        private void btnReplace_Click(object sender, RoutedEventArgs e)
        {
            string ldContent = 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));
            }
        }
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14601556/viewspace-528541/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
IT人士,爱好编程的码农。

注册时间:2008-06-19

  • 博文量
    118
  • 访问量
    692448