0

I am inserting text into an open XML document. The text I retrieve and insert into the document contains HTML formatting, i.e < p >some text< / p > < p >More text< / p > thus the inserted text inside word gets this as text. Can text with HTML get cast to something open XML documents will understand ?

1 Answer 1

1

New answer:

There is actually a project on codeplex that does exactly what you are looking for.

See here the project here:
Html to OpenXml on codeplex

However; if the formatting (headings/paragraphs etc...) are not important you can just strip the HTML-tags entirely.

Here is a tutorial on how to do that:
C# Remove HTML Tags


Old answer (OP worded his question a bit odd, and i misunderstood):

What you need to do is encode your HTML-code somehow; you could use base64 or whatever floats your boat. "Simple" HTML-encoding would probably be the best course of action here.

This way the HTML will not break your XML.

ASP.NET has support for this; but you can do it in any application by importing the required namespace.

Here's an example. HtmlEncode from Class Library

Sign up to request clarification or add additional context in comments.

4 Comments

Hi. I get this result &lt;p&gt;some textt. &lt;/p&gt;&lt;p&gt; This wasn't very XML friendly :) . I used this class: string TestString = "This is a <Test String>."; string EncodedString = System.Web.HttpUtility.HtmlEncode(TestString);
Yes. That can safely be stored in your XML and then be decoded when you need it. :)
I need to decoded it right away :), I developed a button, and on button click an open XML (ms word) document is generated. I create a document using memory stream and write text to on the fly. The strings that I write to the generated document have html formatting. I have tried to remove the formatting by using HthmlEncode class as suggested and tried to strip the html. If i strip the html, the tags are removed, and then I loose the space between paragraphs. If I use HthmlEncode I get special characters in the document. All I want is to have some formatting open XML (Word) can understand.
Ah, I think i have misunderstood you, and you have misunderstood XML. I will update my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.