【问题标题】:Convert wordml (xml) to XHTML/HTML将 wordml (xml) 转换为 XHTML/HTML
【发布时间】:2016-12-30 00:50:16
【问题描述】:

我目前正在研究一种将 wordml-xml(或者更确切地说是正文部分)转换为有效 xhtml/html 格式的方法。原因是我想在我的 WebForms-Application 中正确显示一堆中断、段落等。

在过去的几个小时里,我一直在寻找解决此问题的方法,我发现唯一与我的问题有点相似的是以下博客 (https://msdn.microsoft.com/en-us/library/ff628051(v=office.14).aspx#XHtml_Using)。问题是转换是基于 .docx 而不是 XML。我可以尝试将 XML 转换为 docx 并使用它,但这并不是处理它的有效方法。更不用说我必须先找到一种将 XML 转换为 docx 的方法。

我真的希望有人可以帮助我解决这个问题,因为我有点没有想法。

提前致谢,快点。

示例:XML 中的 w:body-Element 如下所示:

<w:body xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
  <wx:sect xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint">
    <w:p wsp:rsidR="00FF5F75" wsp:rsidRDefault="00626E80" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2">
      <w:r wsp:rsidRPr="00EA67E2">
        <w:rPr>
          <w:rFonts w:fareast="Times New Roman" />
          <w:sz w:val="26" />
          <w:sz-cs w:val="26" />
          <w:lang w:fareast="JA" />
        </w:rPr>
        <w:t>Leider können wir die Kosten für die Impfung gegen %</w:t>
      </w:r>
      <w:r wsp:rsidRPr="00EA67E2">
        <w:rPr>
          <w:rFonts w:fareast="Times New Roman" />
          <w:sz w:val="26" />
          <w:sz-cs w:val="26" />
          <w:highlight w:val="yellow" />
          <w:lang w:fareast="JA" />
        </w:rPr>
        <w:t>XY</w:t>
      </w:r>
      <w:r wsp:rsidRPr="00EA67E2">
        <w:rPr>
          <w:rFonts w:fareast="Times New Roman" />
          <w:sz w:val="26" />
          <w:sz-cs w:val="26" />
          <w:lang w:fareast="JA" />
        </w:rPr>
        <w:t>% nicht übernehmen.</w:t>
      </w:r>
      <w:r wsp:rsidRPr="00EA67E2">
        <w:rPr>
          <w:rFonts w:fareast="Times New Roman" />
          <w:sz w:val="26" />
          <w:sz-cs w:val="26" />
          <w:lang w:fareast="JA" />
        </w:rPr>
        <w:br />
      </w:r>
      <w:r wsp:rsidRPr="00EA67E2">
        <w:rPr>
          <w:rFonts w:fareast="Times New Roman" />
          <w:sz w:val="26" />
          <w:sz-cs w:val="26" />
          <w:lang w:fareast="JA" />
        </w:rPr>
        <w:br />
        <w:t>Die DAK-Gesundheit zahlt Ihnen die Impfungen, die in den Schutzimpfungs-Richtlinien des Gemeinsamen Bundesausschusses genannt sind. Die Impfung gegen %</w:t>
      </w:r>
....

在常规的 word 文档中,这个东西是 Add-in word 的一部分,显示为中断等。我想要将这些元素转换为正确的 HTML/XHTML。

【问题讨论】:

  • 你不能使用 MSOffice API(如果我没记错的话,通过互操作)对文件执行“导出为 HTML”吗?
  • 我很确定它不会那样工作。您需要“docx”格式将其导出为 HTML,为此您需要安装 office,这在 Web 应用程序中并不是那么花哨。

标签: c# html xml xhtml wordml


【解决方案1】:
Try

protected string ConvertXmlToHtmlTable(string xml)
{
  StringBuilder html = new StringBuilder("<table align='center' " + 
     "border='1' class='xmlTable'>\r\n");
  try
  {
      XDocument xDocument = XDocument.Parse(xml);
      XElement root = xDocument.Root;

      var xmlAttributeCollection = root.Elements().Attributes();


      foreach (var ele in root.Elements())
      {
          if (!ele.HasElements)
          {
              string elename = "";
              html.Append("<tr>");

              elename = ele.Name.ToString();

              if (ele.HasAttributes)
              {
                  IEnumerable<XAttribute> attribs = ele.Attributes();
                  foreach (XAttribute attrib in attribs)
                  elename += Environment.NewLine + attrib.Name.ToString() + 
                    "=" + attrib.Value.ToString();
              }

              html.Append("<td>" + elename + "</td>");
              html.Append("<td>" + ele.Value + "</td>");
              html.Append("</tr>");
          }
          else
          {
              string elename = "";
              html.Append("<tr>");

              elename = ele.Name.ToString();

              if (ele.HasAttributes)
              {
                  IEnumerable<XAttribute> attribs = ele.Attributes();
                  foreach (XAttribute attrib in attribs)
                  elename += Environment.NewLine + attrib.Name.ToString() + "=" + attrib.Value.ToString();
              }

              html.Append("<td>" + elename + "</td>");
              html.Append("<td>" + ConvertXmlToHtmlTable(ele.ToString()) + "</td>");
              html.Append("</tr>");
          }
      }

      html.Append("</table>");
  }
  catch (Exception e)
  {
      return xml;
      // Returning the original string incase of error.
  }
  return html.ToString();
}

【讨论】:

  • Ehh .. 我想你不明白我的问题是什么。我会更新OP。无论如何,谢谢。
  • 当然没问题。如果您需要任何帮助,请告诉我。
猜你喜欢
  • 2011-03-09
  • 2014-03-30
  • 1970-01-01
  • 2016-06-12
  • 2012-09-10
  • 2019-03-22
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
相关资源
最近更新 更多