C# FlowDocument 到 HTML 的转换

发布于 2024-11-14 06:06:39 字数 609 浏览 3 评论 0 原文

基本上,我有一个 RichTextBox,我想将其格式化内容转换为 HTML,以便可以将其作为电子邮件发送。

我当前使用的方法根本没有给出任何格式:

string message = new TextRange(messageTextBox.Document.ContentStart,
                               messageTextBox.Document.ContentEnd).Text;

所以我四处搜索并发现 这个,但是,它已有 5 多年的历史,并且在评论中,MSFT 用户评论说不再支持它 - “此示例已从我们的示例中删除设置并不再受支持”,并且它生成的 HTML 格式比现代 HTML 或 XHTML 更旧,最好使用现代 HTML 或 XHTML。

谁能告诉我如何将 RichTextBox 的格式化内容转换为 HTML?

(因此,当电子邮件发送时,收件人会看到带格式的电子邮件)

Basically, I have a RichTextBox and I want to convert the formatted contents of it to HTML so it can be sent as an email.

The method I am currently using does not give any formatting at all:

string message = new TextRange(messageTextBox.Document.ContentStart,
                               messageTextBox.Document.ContentEnd).Text;

So I searched around and found this, however, it is over 5 years old and in the comments an MSFT user has commented saying that it is no longer supported - "This sample has been removed from our sample set and is no longer supported", and the HTML it generates is in an older format than modern HTML or XHTML which would be better to have.

Can anybody show me how I can convert the formatted contents of a RichTextBox to HTML?

(So when the email is sent it the recipient sees the email with formatting)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聚集的泪 2024-11-21 06:06:39

一般技术是使用 XamlWriterFlowDocument 内容转换为 XML 流,然后使用 XSLT 转换将 XML 转换为 HTML。这并不是一个很好的答案,但那是因为任何给定的 FlowDocument 都有大量可能的 HTML 表示形式。

例如,此转换将每个顶级 Section 转换为 div,将每个 Paragraph 转换为 p,以及每个 Run 到一个 span ,其类会告诉您它是斜体、粗体、下划线还是上述的任意组合。它对于我编写它的目的很有用,但将其称为有损转换是一种轻描淡写的说法:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl x">

  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="x:Section[not(parent::x:Section)]">
    <div>
      <xsl:apply-templates select="node()"/>
    </div>
  </xsl:template>

  <xsl:template match="x:Section">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

  <xsl:template match="x:Paragraph">
    <p>
      <xsl:apply-templates select="node()"/>
    </p>
  </xsl:template>

  <xsl:template match="x:Run">
    <xsl:variable name="class">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>i </xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>b </xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>u </xsl:text>
      </xsl:if>
    </xsl:variable>
    <span>
      <xsl:if test="normalize-space($class) != ''">
        <xsl:attribute name="class">
          <xsl:value-of select="normalize-space($class)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
    </span>
  </xsl:template>

</xsl:stylesheet>

这是我编写的用于执行转换的值转换器 - 请注意,为了使用值转换器,您还必须破解并实现将内容公开为依赖属性的 RichTextBox 版本。确实,整个项目很痛苦。

public class FlowDocumentToHtmlConverter : IValueConverter
{
    private static XslCompiledTransform ToHtmlTransform;
    private static XslCompiledTransform ToXamlTransform;

    public FlowDocumentToHtmlConverter()
    {
        if (ToHtmlTransform == null)
        {
            ToHtmlTransform = LoadTransformResource("Converters/FlowDocumentToXhtml.xslt");
        }
        if (ToXamlTransform == null)
        {
            ToXamlTransform = LoadTransformResource("Converters/XhtmlToFlowDocument.xslt");
        }
    }
    private static XslCompiledTransform LoadTransformResource(string path)
    {
        Uri uri = new Uri(path, UriKind.Relative);
        XmlReader xr = XmlReader.Create(Application.GetResourceStream(uri).Stream);
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(xr);
        return xslt;
    }

    #region IValueConverter Members

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!(value is FlowDocument))
        {
            return null;
        }
        if (targetType == typeof(FlowDocument))
        {
            return value;
        }

        if (targetType != typeof(string))
        {
            throw new InvalidOperationException(
                "FlowDocumentToHtmlConverter can only convert back from a FlowDocument to a string.");
        }

        FlowDocument d = (FlowDocument)value;

        using (MemoryStream ms = new MemoryStream())
        {
            // write XAML out to a MemoryStream
            TextRange tr = new TextRange(
                d.ContentStart,
                d.ContentEnd);
            tr.Save(ms, DataFormats.Xaml);
            ms.Seek(0, SeekOrigin.Begin);

            // transform the contents of the MemoryStream to HTML
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                XmlWriterSettings xws = new XmlWriterSettings();
                xws.OmitXmlDeclaration = true;
                XmlReader xr = XmlReader.Create(ms);
                XmlWriter xw = XmlWriter.Create(sw, xws);
                ToHtmlTransform.Transform(xr, xw);
            }
            return sb.ToString();
        }
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
        {
            return new FlowDocument();
        }
        if (value is FlowDocument)
        {
            return value;
        }
        if (targetType != typeof(FlowDocument))
        {
            throw new InvalidOperationException(
                "FlowDocumentToHtmlConverter can only convert to a FlowDocument.");
        }
        if (!(value is string))
        {
            throw new InvalidOperationException(
                "FlowDocumentToHtmlConverter can only convert from a string or FlowDocument.");
        }

        string s = (string)value;

        FlowDocument d;

        using (MemoryStream ms = new MemoryStream())
        using (StringReader sr = new StringReader(s))
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.OmitXmlDeclaration = true;
            using (XmlReader xr = XmlReader.Create(sr))
            using (XmlWriter xw = XmlWriter.Create(ms, xws))
            {
                ToXamlTransform.Transform(xr, xw);
            }
            ms.Seek(0, SeekOrigin.Begin);

            d = XamlReader.Load(ms) as FlowDocument;
        }
        XamlWriter.Save(d, Console.Out);
        return d;
    }

    #endregion
}

The general technique is to use a XamlWriter to convert the FlowDocument content to a stream of XML, and then to use an XSLT transform to convert the XML to HTML. That's not much of an answer, but that's because there's a huge range of possible HTML representations of any given FlowDocument.

This transform, for instance, converts every top-level Section to a div, every Paragraph to a p, and every Run to a span whose class tells you whether or not it's italicized, bold-faced, or underlined, or any combination of the above. It was useful for the purpose I wrote it for, but to call it a lossy transformation is an understatement:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl x">

  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="x:Section[not(parent::x:Section)]">
    <div>
      <xsl:apply-templates select="node()"/>
    </div>
  </xsl:template>

  <xsl:template match="x:Section">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

  <xsl:template match="x:Paragraph">
    <p>
      <xsl:apply-templates select="node()"/>
    </p>
  </xsl:template>

  <xsl:template match="x:Run">
    <xsl:variable name="class">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>i </xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>b </xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>u </xsl:text>
      </xsl:if>
    </xsl:variable>
    <span>
      <xsl:if test="normalize-space($class) != ''">
        <xsl:attribute name="class">
          <xsl:value-of select="normalize-space($class)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
    </span>
  </xsl:template>

</xsl:stylesheet>

Here's a value converter I wrote to do the conversion - note that in order to use the value converter, you also have to hack around and implement a version of RichTextBox that exposes the content as a dependency property. Really this whole project was a pain.

public class FlowDocumentToHtmlConverter : IValueConverter
{
    private static XslCompiledTransform ToHtmlTransform;
    private static XslCompiledTransform ToXamlTransform;

    public FlowDocumentToHtmlConverter()
    {
        if (ToHtmlTransform == null)
        {
            ToHtmlTransform = LoadTransformResource("Converters/FlowDocumentToXhtml.xslt");
        }
        if (ToXamlTransform == null)
        {
            ToXamlTransform = LoadTransformResource("Converters/XhtmlToFlowDocument.xslt");
        }
    }
    private static XslCompiledTransform LoadTransformResource(string path)
    {
        Uri uri = new Uri(path, UriKind.Relative);
        XmlReader xr = XmlReader.Create(Application.GetResourceStream(uri).Stream);
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(xr);
        return xslt;
    }

    #region IValueConverter Members

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!(value is FlowDocument))
        {
            return null;
        }
        if (targetType == typeof(FlowDocument))
        {
            return value;
        }

        if (targetType != typeof(string))
        {
            throw new InvalidOperationException(
                "FlowDocumentToHtmlConverter can only convert back from a FlowDocument to a string.");
        }

        FlowDocument d = (FlowDocument)value;

        using (MemoryStream ms = new MemoryStream())
        {
            // write XAML out to a MemoryStream
            TextRange tr = new TextRange(
                d.ContentStart,
                d.ContentEnd);
            tr.Save(ms, DataFormats.Xaml);
            ms.Seek(0, SeekOrigin.Begin);

            // transform the contents of the MemoryStream to HTML
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                XmlWriterSettings xws = new XmlWriterSettings();
                xws.OmitXmlDeclaration = true;
                XmlReader xr = XmlReader.Create(ms);
                XmlWriter xw = XmlWriter.Create(sw, xws);
                ToHtmlTransform.Transform(xr, xw);
            }
            return sb.ToString();
        }
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
        {
            return new FlowDocument();
        }
        if (value is FlowDocument)
        {
            return value;
        }
        if (targetType != typeof(FlowDocument))
        {
            throw new InvalidOperationException(
                "FlowDocumentToHtmlConverter can only convert to a FlowDocument.");
        }
        if (!(value is string))
        {
            throw new InvalidOperationException(
                "FlowDocumentToHtmlConverter can only convert from a string or FlowDocument.");
        }

        string s = (string)value;

        FlowDocument d;

        using (MemoryStream ms = new MemoryStream())
        using (StringReader sr = new StringReader(s))
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.OmitXmlDeclaration = true;
            using (XmlReader xr = XmlReader.Create(sr))
            using (XmlWriter xw = XmlWriter.Create(ms, xws))
            {
                ToXamlTransform.Transform(xr, xw);
            }
            ms.Seek(0, SeekOrigin.Begin);

            d = XamlReader.Load(ms) as FlowDocument;
        }
        XamlWriter.Save(d, Console.Out);
        return d;
    }

    #endregion
}
埖埖迣鎅 2024-11-21 06:06:39

我认为上面的 XSLT 表的扩展版本可能会有所帮助。不完美但稍微更全面。引用此答案的另一个答案的扩展的扩展。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl x">

  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <!--<xsl:template match="x:Section[not(parent::x:Section)]">
    <div>
      <xsl:apply-templates select="node()"/>
    </div>
  </xsl:template>-->

  <xsl:template match="x:Section[not(parent::x:Section)]">
    <xsl:variable name="style">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>font-style:italic;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>font-weight:bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>text-decoration:underline;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontSize != ''">
        <xsl:text>font-size:</xsl:text>
        <xsl:value-of select="@FontSize" />
        <xsl:text>pt;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontFamily != ''">
        <xsl:text>font-family:</xsl:text>
        <xsl:value-of select="@FontFamily" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="concat(substring(@Foreground, 1, 1), substring(@Foreground, 4))" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground-Color != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="@Foreground-Color"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <div>
      <xsl:if test="normalize-space($style) != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="normalize-space($style)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
      <xsl:apply-templates select="node()"/>
    </div>
  </xsl:template>


  <xsl:template match="x:Section">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

  <xsl:template match="x:Paragraph">
    <xsl:variable name="style">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>font-style:italic;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>font-weight:bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>text-decoration:underline;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontSize != ''">
        <xsl:text>font-size:</xsl:text>
        <xsl:value-of select="@FontSize" />
        <xsl:text>pt;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontFamily != ''">
        <xsl:text>font-family:</xsl:text>
        <xsl:value-of select="@FontFamily" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="concat(substring(@Foreground, 1, 1), substring(@Foreground, 4))" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground-Color != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="@Foreground-Color"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <p>
      <xsl:if test="normalize-space($style) != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="normalize-space($style)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
      <xsl:apply-templates select="node()"/>
    </p>
  </xsl:template>

  <xsl:template match="x:Span">
    <xsl:variable name="style">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>font-style:italic;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>font-weight:bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>text-decoration:underline;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontSize != ''">
        <xsl:text>font-size:</xsl:text>
        <xsl:value-of select="@FontSize" />
        <xsl:text>pt;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontFamily != ''">
        <xsl:text>font-family:</xsl:text>
        <xsl:value-of select="@FontFamily" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="concat(substring(@Foreground, 1, 1), substring(@Foreground, 4))" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground-Color != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="@Foreground-Color"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <span>
      <xsl:if test="normalize-space($style) != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="normalize-space($style)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
      <xsl:apply-templates select="node()"/>
    </span>
  </xsl:template>


  <xsl:template match="x:Run">
    <xsl:variable name="style">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>font-style:italic;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>font-weight:bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>text-decoration:underline;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontSize != ''">
        <xsl:text>font-size:</xsl:text>
        <xsl:value-of select="@FontSize" />
        <xsl:text>pt;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontFamily != ''">
        <xsl:text>font-family:</xsl:text>
        <xsl:value-of select="@FontFamily" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="concat(substring(@Foreground, 1, 1), substring(@Foreground, 4))" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground-Color != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="@Foreground-Color"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <span>
      <xsl:if test="normalize-space($style) != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="normalize-space($style)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
      <xsl:apply-templates select="node()"/>
    </span>
  </xsl:template>
</xsl:stylesheet>

Extended version of XSLT sheet above I thought might help. Not perfect but slightly more comprehensive. Extension of an extension of another answer referencing this one.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl x">

  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <!--<xsl:template match="x:Section[not(parent::x:Section)]">
    <div>
      <xsl:apply-templates select="node()"/>
    </div>
  </xsl:template>-->

  <xsl:template match="x:Section[not(parent::x:Section)]">
    <xsl:variable name="style">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>font-style:italic;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>font-weight:bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>text-decoration:underline;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontSize != ''">
        <xsl:text>font-size:</xsl:text>
        <xsl:value-of select="@FontSize" />
        <xsl:text>pt;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontFamily != ''">
        <xsl:text>font-family:</xsl:text>
        <xsl:value-of select="@FontFamily" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="concat(substring(@Foreground, 1, 1), substring(@Foreground, 4))" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground-Color != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="@Foreground-Color"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <div>
      <xsl:if test="normalize-space($style) != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="normalize-space($style)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
      <xsl:apply-templates select="node()"/>
    </div>
  </xsl:template>


  <xsl:template match="x:Section">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

  <xsl:template match="x:Paragraph">
    <xsl:variable name="style">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>font-style:italic;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>font-weight:bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>text-decoration:underline;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontSize != ''">
        <xsl:text>font-size:</xsl:text>
        <xsl:value-of select="@FontSize" />
        <xsl:text>pt;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontFamily != ''">
        <xsl:text>font-family:</xsl:text>
        <xsl:value-of select="@FontFamily" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="concat(substring(@Foreground, 1, 1), substring(@Foreground, 4))" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground-Color != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="@Foreground-Color"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <p>
      <xsl:if test="normalize-space($style) != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="normalize-space($style)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
      <xsl:apply-templates select="node()"/>
    </p>
  </xsl:template>

  <xsl:template match="x:Span">
    <xsl:variable name="style">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>font-style:italic;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>font-weight:bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>text-decoration:underline;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontSize != ''">
        <xsl:text>font-size:</xsl:text>
        <xsl:value-of select="@FontSize" />
        <xsl:text>pt;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontFamily != ''">
        <xsl:text>font-family:</xsl:text>
        <xsl:value-of select="@FontFamily" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="concat(substring(@Foreground, 1, 1), substring(@Foreground, 4))" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground-Color != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="@Foreground-Color"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <span>
      <xsl:if test="normalize-space($style) != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="normalize-space($style)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
      <xsl:apply-templates select="node()"/>
    </span>
  </xsl:template>


  <xsl:template match="x:Run">
    <xsl:variable name="style">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>font-style:italic;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>font-weight:bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>text-decoration:underline;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontSize != ''">
        <xsl:text>font-size:</xsl:text>
        <xsl:value-of select="@FontSize" />
        <xsl:text>pt;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontFamily != ''">
        <xsl:text>font-family:</xsl:text>
        <xsl:value-of select="@FontFamily" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="concat(substring(@Foreground, 1, 1), substring(@Foreground, 4))" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground-Color != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="@Foreground-Color"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <span>
      <xsl:if test="normalize-space($style) != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="normalize-space($style)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
      <xsl:apply-templates select="node()"/>
    </span>
  </xsl:template>
</xsl:stylesheet>
忆依然 2024-11-21 06:06:39

对于那些正在寻找 .Net Core (APS.Net Core) 解决方案的人 - nuget MarkupConverter 为我解决了这个问题。添加依赖引用

<PackageReference Include="MarkupConverter" Version="1.0.6" />

然后使用

MarkupConverter.MarkupConverter markupConverter = new MarkupConverter.MarkupConverter();
try
{
  message = markupConverter.ConvertXamlToHtml(message);
}
catch (Exception ex)
{
  _logger.LogError(ex, $"Failed to parse flowdocument");
}

For those who is looking for solution for .Net Core (APS.Net Core) - nuget MarkupConverter did the trick for me. Add dependency reference

<PackageReference Include="MarkupConverter" Version="1.0.6" />

Then use it

MarkupConverter.MarkupConverter markupConverter = new MarkupConverter.MarkupConverter();
try
{
  message = markupConverter.ConvertXamlToHtml(message);
}
catch (Exception ex)
{
  _logger.LogError(ex, 
quot;Failed to parse flowdocument");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文