郁闷了大半天,终于搞定了,把用户输入的内容转换成XML格式信息,在内存中创建XmlDocument...

  /// <summary>
  /// 在内存中以XML格式生成所有用户输入信息
  /// </summary>
  /// <returns>返回XmlDocument对象</returns>
  private XmlDocument SaveToXml()
  {
   this.xmlString = "<TextFormatInfo>";
   PreFontName = this.EditBox.SelectionFont.Name;//前一个字的字体
   PreFontSize = this.EditBox.SelectionFont.Size;//前一个字大小
   PreColor = this.EditBox.SelectionColor.Name;//前一个字的颜色
   PreAlignment = this.EditBox.SelectionAlignment.ToString();//前一个字的对齐方式
   PreBold = this.EditBox.SelectionFont.Bold;//前一个字是否为粗体
   PreItalic = this.EditBox.SelectionFont.Italic;//前一个字是否为斜体
   PreUnderLine = this.EditBox.SelectionFont.Underline;//前一个字是否有下划线

   //初始化第一个字的格式信息
   xmlString += "<Text Font='"+PreFontName+"' Size='"+PreFontSize.ToString()+"' Color='"+PreColor+"' Alignment='"+PreAlignment+"' Bold='"+PreBold.ToString()+"' Italic='"+PreItalic.ToString()+"' UnderLine='"+PreUnderLine.ToString()+"'>";
   textInfo = this.EditBox.SelectedText;

   //遍历输入的所有内容
   for(int i=0;i<this.EditBox.Text.Length;i++)
   {
    this.EditBox.Select(i,1);
    if(this.EditBox.SelectionFont.Name != PreFontName)//如果前后两个字体不同
    {
     textInfo = this.EditBox.SelectedText;
     PreFontName = this.EditBox.SelectionFont.Name;
     PreFontSize = this.EditBox.SelectionFont.Size;
     PreColor = this.EditBox.SelectionColor.Name;
     PreAlignment = this.EditBox.SelectionAlignment.ToString();
     PreBold = this.EditBox.SelectionFont.Bold;
     PreItalic = this.EditBox.SelectionFont.Italic;
     PreUnderLine = this.EditBox.SelectionFont.Underline;

     xmlString += "</Text><Text Font='"+PreFontName+"' Size='"+PreFontSize.ToString()+"' Color='"+PreColor+"' Alignment='"+PreAlignment+"' Bold='"+PreBold.ToString()+"' Italic='"+PreItalic.ToString()+"' UnderLine='"+PreUnderLine.ToString()+"'>";
     xmlString += textInfo;
    }
    else
    {
     if(this.EditBox.SelectionFont.Size != PreFontSize)//如果前后两个大小不同
     {
      textInfo = this.EditBox.SelectedText;
      PreFontName = this.EditBox.SelectionFont.Name;
      PreFontSize = this.EditBox.SelectionFont.Size;
      PreColor = this.EditBox.SelectionColor.Name;
      PreAlignment = this.EditBox.SelectionAlignment.ToString();
      PreBold = this.EditBox.SelectionFont.Bold;
      PreItalic = this.EditBox.SelectionFont.Italic;
      PreUnderLine = this.EditBox.SelectionFont.Underline;

      xmlString += "</Text><Text Font='"+PreFontName+"' Size='"+PreFontSize.ToString()+"' Color='"+PreColor+"' Alignment='"+PreAlignment+"' Bold='"+PreBold.ToString()+"' Italic='"+PreItalic.ToString()+"' UnderLine='"+PreUnderLine.ToString()+"'>";
      xmlString += textInfo;
     }
     else
     {
      if(this.EditBox.SelectionColor.Name != PreColor)//如果前后两个颜色不同
      {
       textInfo = this.EditBox.SelectedText;
       PreFontName = this.EditBox.SelectionFont.Name;
       PreFontSize = this.EditBox.SelectionFont.Size;
       PreColor = this.EditBox.SelectionColor.Name;
       PreAlignment = this.EditBox.SelectionAlignment.ToString();
       PreBold = this.EditBox.SelectionFont.Bold;
       PreItalic = this.EditBox.SelectionFont.Italic;
       PreUnderLine = this.EditBox.SelectionFont.Underline;

       xmlString += "</Text><Text Font='"+PreFontName+"' Size='"+PreFontSize.ToString()+"' Color='"+PreColor+"' Alignment='"+PreAlignment+"' Bold='"+PreBold.ToString()+"' Italic='"+PreItalic.ToString()+"' UnderLine='"+PreUnderLine.ToString()+"'>";
       xmlString += textInfo;
      }
      else
      {
       if(this.EditBox.SelectionAlignment.ToString() != PreAlignment)//如果前后两个对齐方式不同
       {
        textInfo = this.EditBox.SelectedText;
        PreFontName = this.EditBox.SelectionFont.Name;
        PreFontSize = this.EditBox.SelectionFont.Size;
        PreColor = this.EditBox.SelectionColor.Name;
        PreAlignment = this.EditBox.SelectionAlignment.ToString();
        PreBold = this.EditBox.SelectionFont.Bold;
        PreItalic = this.EditBox.SelectionFont.Italic;
        PreUnderLine = this.EditBox.SelectionFont.Underline;

        xmlString += "</Text><Text Font='"+PreFontName+"' Size='"+PreFontSize.ToString()+"' Color='"+PreColor+"' Alignment='"+PreAlignment+"' Bold='"+PreBold.ToString()+"' Italic='"+PreItalic.ToString()+"' UnderLine='"+PreUnderLine.ToString()+"'>";
        xmlString += textInfo;
       }
       else
       {
        if(this.EditBox.SelectionFont.Bold != PreBold)
        {      
         textInfo = this.EditBox.SelectedText;
         PreFontName = this.EditBox.SelectionFont.Name;
         PreFontSize = this.EditBox.SelectionFont.Size;
         PreColor = this.EditBox.SelectionColor.Name;
         PreAlignment = this.EditBox.SelectionAlignment.ToString();
         PreBold = this.EditBox.SelectionFont.Bold;
         PreItalic = this.EditBox.SelectionFont.Italic;
         PreUnderLine = this.EditBox.SelectionFont.Underline;

         xmlString += "</Text><Text Font='"+PreFontName+"' Size='"+PreFontSize.ToString()+"' Color='"+PreColor+"' Alignment='"+PreAlignment+"' Bold='"+PreBold.ToString()+"' Italic='"+PreItalic.ToString()+"' UnderLine='"+PreUnderLine.ToString()+"'>";
         xmlString += textInfo;
        }
        else
        {
         if(this.EditBox.SelectionFont.Italic != PreItalic)
         { 
          textInfo = this.EditBox.SelectedText;
          PreFontName = this.EditBox.SelectionFont.Name;
          PreFontSize = this.EditBox.SelectionFont.Size;
          PreColor = this.EditBox.SelectionColor.Name;
          PreAlignment = this.EditBox.SelectionAlignment.ToString();
          PreBold = this.EditBox.SelectionFont.Bold;
          PreItalic = this.EditBox.SelectionFont.Italic;
          PreUnderLine = this.EditBox.SelectionFont.Underline;

          xmlString += "</Text><Text Font='"+PreFontName+"' Size='"+PreFontSize.ToString()+"' Color='"+PreColor+"' Alignment='"+PreAlignment+"' Bold='"+PreBold.ToString()+"' Italic='"+PreItalic.ToString()+"' UnderLine='"+PreUnderLine.ToString()+"'>";
          xmlString += textInfo;
         }
         else
         {
          if(this.EditBox.SelectionFont.Underline != PreUnderLine)
          {      
           textInfo = this.EditBox.SelectedText;
           PreFontName = this.EditBox.SelectionFont.Name;
           PreFontSize = this.EditBox.SelectionFont.Size;
           PreColor = this.EditBox.SelectionColor.Name;
           PreAlignment = this.EditBox.SelectionAlignment.ToString();
           PreBold = this.EditBox.SelectionFont.Bold;
           PreItalic = this.EditBox.SelectionFont.Italic;
           PreUnderLine = this.EditBox.SelectionFont.Underline;

           xmlString += "</Text><Text Font='"+PreFontName+"' Size='"+PreFontSize.ToString()+"' Color='"+PreColor+"' Alignment='"+PreAlignment+"' Bold='"+PreBold.ToString()+"' Italic='"+PreItalic.ToString()+"' UnderLine='"+PreUnderLine.ToString()+"'>";
           xmlString += textInfo;
          }
          else
          {
           textInfo = this.EditBox.SelectedText;
           xmlString += textInfo;
          }
         }
        }
       }
      }
     }
    }
    this.EditBox.Select(i+1,0);
   }

   XmlDocument doc = new XmlDocument();

   this.XmlTextBox.Text = xmlString + "</Text></TextFormatInfo>";

   doc.LoadXml(xmlString+"</Text></TextFormatInfo>");

   return doc;
  }

转载于:https://www.cnblogs.com/xleelee/archive/2004/05/19/10342.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值