7

I am trying to use Linq to XML to save & retrieve some HTML between an XML file and a windows forms application. When it saves it to the XML file the HTML tags get xml encoded and it isn't saved as straight HTML.

Example HTML:

<P><FONT color=#004080><U>Sample HTML</U></FONT></P>

Saved in XML File:

&lt;P&gt;&lt;FONT color=#004080&gt;&lt;U&gt;Sample HTML&lt;/U&gt;&lt;/FONT&gt;&lt;/P&gt;

When I manually edit the XML file and put in the desired HTML the Linq pulls in the HTML and displays it properly.

Here is the code that saves the HTML to the XML file:

XElement currentReport = (from item in callReports.Descendants("callReport")
                                  where (int)item.Element("localId") == myCallreports.LocalId
                                  select item).FirstOrDefault();

        currentReport.Element("studio").Value = myCallreports.Studio;
        currentReport.Element("visitDate").Value = myCallreports.Visitdate.ToShortDateString();
       // *** The next two XElements store the HTML
        currentReport.Element("recomendations").Value = myCallreports.Comments;
        currentReport.Element("reactions").Value = myCallreports.Ownerreaction;

I assume this is happening b/c of xml encoding but I am not sure how to deal with it. This question gave me some clues...but no answer (for me, at least).

Thanks for the help,

Oran

2 Answers 2

5

Setting the Value property will automatically encode the html string. This should do the trick, but you'll need to make sure that your HTML is valid XML (XHTML).

currentReport.Element("recomendations").ReplaceNodes(XElement.Parse(myCallreports.Comments));

Edit: You may need to wrap the user entered HTML in <div> </div> tags. XElement.Parse expects to find a string with at least a start and end xml tag. So, this might work better:

currentReport.Element("recomendations").ReplaceNodes(XElement.Parse("<div>" + myCallreports.Comments + "</div>"));

Then you just have to make sure that tags like <br> are being sent in as <br />.

Edit 2: The other option would be use XML CDATA. Wrap the HTML with <![CDATA[ and ]]>, but I've never actually used that and I'm not sure how it affects reading the xml.

currentReport.Element("recomendations").ReplaceNodes(XElement.Parse("<![CDATA[" + myCallreports.Comments + "]]>"));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. It worked! But now I am having a problem validating the HTML. I am using this control codeproject.com/KB/edit/editor_in_windows_forms.aspx to allow users to create the HTML and it doesn't seem to have valid XHTML. I guess I have to find a new HTML control, format the HTML in code or use another method to save the HTML in XML. Any Suggestions?
I had already tried wrapping the html in a tag but the problem is that the HTML Editor control I am using produces invalid XHTML
Thank you very much Dennis! CData works! Here is the Linq To XML usage of CData currentReport.Element("recomendations").ReplaceNodes(new XCData(myRbccallreports.Comments));
-1

Try using currentReport.Element("studio").InnerXml instead of currentReport.Element("studio").Value

1 Comment

I don't see InnerXml listed as a member of XElement. msdn.microsoft.com/en-us/library/…

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.