I'm using the new System.Xml.Linq to create HTML documents (Yes, I know about HtmlDocument, but much prefer the XDocument/XElement classes). I'm having a problem inserting
(or any other HTML entity). What I've tried already:
Just putting text in directly doesn't work because the & gets turned int &.
new XElement("h1", "Text to keep together.");
I tried parsing in the raw XML using the following, but it barfs with this error:
XElement.Parse("Text to keep together.");
--> Reference to undeclared entity 'nbsp'.`
Try number three looks like the following. If I save to a file, there is just a space, the  
gets lost.
var X = new XDocument(new XElement("Name", KeepTogether("Hi Mom!")));
private static XNode KeepTogether(string p)`
{
return XElement.Parse("<xml>" + p.Replace(" ", " ") + "</xml>").FirstNode;
}
I couldn't find a way to just shove the raw text through without it getting escaped. Am I missing something obvious?
I couldn't find a way to just shove the raw text through without it getting escaped.
Just put the Unicode character in that refers to (U+00A0 NO-BREAK SPACE) directly in a text node, then let the serializer worry about whether it needs escaping to   or not. (Probably not: if you are using UTF-8 or ISO-8859-1 as your page encoding the character can be included directly without having to worry about encoding it into an entity reference or character reference).
new XElement("h1", "Text\u00A0to\u00A0keep\u00A0together");
Replacing the & with a marker like ##AMP## solved my problem. Maybe not the prettiest solution but I got a demo for a customer in 10 mins so...I don't care :)
Thx for the idea
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With