MS Word格式化文本到XML C#
问题描述:
如何使用asp.net将格式化MS Word文本转换为XML。
实际上问题是当我们通过文本框中的Word文本保存到XML数据时类型。保存XML解析器时抛出异常。
我想在Text下面转换为纯XML:
How to convert Formatted MS Word text to XML using asp.net.
Actually the issue is when we past the Word text inside textbox to save to XML data type. and while saving XML parser throws the exception.
I want to convert below Text to Pure XML:
<p><font face="Times New Roman" size="3">
</font></p><p class="MsoNormal" style="margin: 0in 0in 10pt;"><b><span lang="DA" style='line-height: 115%; font-family: "Tahoma","sans-serif"; font-size: 8.5pt;'>Hello Dummy text need XML n 18. juni 2015.</span></b><span lang="DA" style='line-height: 115%; font-family: "Tahoma","sans-serif"; font-size: 8.5pt;'><br>
<br>
demo text for experiment være
opmærksom på følgende regler:<br>
<br>
<b>1. Fl hello texten</b><br>
<br>
Street 101 Indiae adresse.<br>
<br>
答
您不能只将文本粘贴到XML文件中。您必须将文本编码为CDATA部分,因此不会使用XML格式。
Google的C#CDATA部分示例。
You can't just paste the text into an XML file. You have to encode the text into a CDATA section so it doesn't screw with the XML.
Google for "C# CDATA section" for examples.
作为 Dave Kreskoviak [ ^ ]提及(解决方案1),您必须包含HTML内容进入CDATA部分。
As Dave Kreskoviak[^] mentioned (solution 1), you have to include HTML content into CDATA section.
string HtmlText = @"<p><font face="Times New Roman" size="3">
</font></p><p class="MsoNormal" style="margin: 0in 0in 10pt;"><span lang="DA" style="line-height: 115%; font-family: " tahoma="," sans-serif="; font-size: 8.5pt;">Hello Dummy text need XML n 18. juni 2015.</span><span lang="DA" style="line-height: 115%; font-family: " tahoma="," sans-serif="; font-size: 8.5pt;"><br>
<br>
demo text for experiment være
opmærksom på følgende regler:<br>
<br>
1. Fl hello texten<br>
<br>
Street 101 Indiae adresse.<br>
<br>";
XDocument xdoc = new XDocument();
XElement xroot = new XElement("MyXml");
XElement xdata = new XElement("MyData", new XCData(HtmlText));
xroot.Add(xdata);
xdoc.Add(xroot);
//xdoc.Save("Enter_full_file_name");</br></br></br></br></br></br></br></br></span></p>
结果:
Result:
<MyXml>
<MyData><![CDATA[<p><font face='Times New Roman' size='3'>
</font></p><p class='MsoNormal' style='margin: 0in 0in 10pt;'><b><span lang='DA' style='line-height: 115%; font-family: 'Tahoma','sans-serif'; font-size: 8.5pt;'>Hello Dummy text need XML n 18. juni 2015.</span></b><span lang='DA' style='line-height: 115%; font-family: 'Tahoma','sans-serif'; font-size: 8.5pt;'><br>
<br>
demo text for experiment være
opmærksom på følgende regler:<br>
<br>
<b>1. Fl hello texten</b><br>
<br>
Street 101 Indiae adresse.<br>
<br>]]></MyData>
</MyXml>
否则,您将被迫将每个<
和>
替换为:
< and >