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.