html 转 wordml js函数,将Wordml(XML)转换为XHTML / HTML

I'm currently working on a way to convert a wordml-xml (or rather the body-part) into a valid xhtml/html format. The reason for that is that there are a bunch of breaks, paragraphs and so on that I want to display properly in my WebForms-Application.

I've been searching for ways to do this for the past few hours and the only thing I found that somewhat resembles my issues is the following Blog (https://msdn.microsoft.com/en-us/library/ff628051(v=office.14).aspx#XHtml_Using). The problem is that the transformation is based on .docx and not on XML. I could try to convert the XML into a docx and work with that, but that wouldn't really be an effective way to deal with it. not to mention that I'd have to find a way to convert the XML into docx first.

I really hope that somebody out there can help me with this, because I'm somewhat out of ideas.

Thanks in advance, snap.

Example: The w:body-Element inside of the XML looks like this:

Leider können wir die Kosten für die Impfung gegen %

XY

% nicht übernehmen.

Die DAK-Gesundheit zahlt Ihnen die Impfungen, die in den Schutzimpfungs-Richtlinien des Gemeinsamen Bundesausschusses genannt sind. Die Impfung gegen %

....

In a regular word-document, where this thing is part of an Add-in word Displays as a break etc. What I want is to convert these elements to proper HTML/XHTML.

Talk1:

Can't you use the MSOffice API (through interop if I remember correctly) to perform an "export as HTML" on the file?

Talk2:

I'm pretty sure it doesn't work that way. You need the "docx"-Format to export it to HTML and for that you need to have office installed which isn't really all that fancy in a web application.

Solutions1Try

protected string ConvertXmlToHtmlTable(string xml)

{

StringBuilder html = new StringBuilder("

"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("

");

elename = ele.Name.ToString();

if (ele.HasAttributes)

{

IEnumerable attribs = ele.Attributes();

foreach (XAttribute attrib in attribs)

elename += Environment.NewLine + attrib.Name.ToString() +

"=" + attrib.Value.ToString();

}

html.Append("

" + elename + "");

html.Append("

" + ele.Value + "");

html.Append("

");

}

else

{

string elename = "";

html.Append("

");

elename = ele.Name.ToString();

if (ele.HasAttributes)

{

IEnumerable attribs = ele.Attributes();

foreach (XAttribute attrib in attribs)

elename += Environment.NewLine + attrib.Name.ToString() + "=" + attrib.Value.ToString();

}

html.Append("

" + elename + "");

html.Append("

" + ConvertXmlToHtmlTable(ele.ToString()) + "");

html.Append("

");

}

}

html.Append("

");

}

catch (Exception e)

{

return xml;

// Returning the original string incase of error.

}

return html.ToString();

}

Talk1:

Ehh .. I don't think you understood what my problem is. I'll update the OP. Thanks anywas.

Talk2:

Sure no problems. Let me know if you need any help.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值