views:

1634

answers:

2

Hi,

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

A: 

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

James Curran
I don't see InnerXml listed as a member of XElement. http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement_members.aspx
Dennis Palmer
+3  A: 

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 + "]]>"));
Dennis Palmer
Thanks for the answer. It worked! But now I am having a problem validating the HTML. I am using this control http://www.codeproject.com/KB/edit/editor_in_windows_forms.aspxto 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?
orandov
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
orandov
Thank you very much Dennis! CData works! Here is the Linq To XML usage of CData currentReport.Element("recomendations").ReplaceNodes(new XCData(myRbccallreports.Comments));
orandov
That's cool! Nice to know about the XCData class.
Dennis Palmer