Click here to Skip to main content
15,886,771 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
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:

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>
Posted
Updated 3-Jun-15 0:28am
v2
Comments
Mukesh Pr@sad 2-Jun-15 9:14am    
Please post your code so that we can understand where the problem is arising..
Shagun Bansal 3-Jun-15 6:29am    
Added code, I need to create XML from it using c#
Maciej Los 2-Jun-15 9:17am    
What have you tried? Where are you stuck?
Shagun Bansal 3-Jun-15 6:29am    
Added code, I need to create XML from it using c#
Maciej Los 3-Jun-15 7:16am    
What is expected output (pure XML)?

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.
 
Share this answer
 
Comments
Shagun Bansal 3-Jun-15 6:29am    
Added code, I need to create XML from it using c#
Dave Kreskowiak 3-Jun-15 7:40am    
That's not code and I already told you what you have to do.
As Dave Kreskoviak[^] mentioned (solution 1), you have to include HTML content into CDATA section.

C#
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:
XML
<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>


Otherwise, you'll be forced to replace each < and > into:
HTML
< and >
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900