tags:

views:

1743

answers:

8

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:

  1. Just putting text in directly doesn't work because the & gets turned int &. new XElement("h1", "Text to keep together.");

  2. 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'.`

  3. 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(" ", "&#160;") + "</xml>").FirstNode;
}

I couldn't find a way to just shove the raw text through without it getting escaped. Am I missing something obvious?

+1  A: 

You could also try using numbered entities, they need no declaration. Numbered entity equivalent to the named entity &nbsp; is &#160;

baretta
BCS
+1  A: 
codemeit
This allows parsing in the XML. However, if I add do an x.Save("test.xml"), the output file has a space character instead of   between the two test words. Argh!!!
Eric
codemeit
I'm creating a large html document, and then do a doc.Save(). This location is deep in the document, and not easily findable. For now I'm going to hack it and remove the space (its between a time and AM/PM). 6:30PM will be fine. Thanks for your help, though!
Eric
+6  A: 

I couldn't find a way to just shove the raw text through without it getting escaped.

Just put the Unicode character in that &nbsp; refers to (U+00A0 NO-BREAK SPACE) directly in a text node, then let the serializer worry about whether it needs escaping to &#160; 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");
bobince
A: 
&amp;nbsp;
Brian Dilley
This will result in: ``
Adam Nofsinger
A: 

If you want to be safe adding HTML in your XML you have to use CDATA

string xml = "<xml><![CDATA[All the HTML you want]]></xml>";
Eduardo Molteni
This won't work either, because .NET will encode all of the `<!>/` characters, so you'll end up with something like `<![CDATA[all html you want]]>`
Adam Nofsinger
A: 

I came up with this slightly daft approach which suits me: String replace all the & with ##AMP## when you store the data.... And reverse that operation on output. I am using this in conjunction with XElement SQL column and works a treat.

Regards

Neil

+1  A: 

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

Daniel
Seems to be the only way I have found either, when you REALLY need the actual ouput string to have, for instance, an `` in it and not an actual newline or ``
Adam Nofsinger
A: 

I know this is old, but I found something and I'm rather surprised!

XElement element = new XElement("name",new XCData("<br /> & etc"));

And there you go! CDATA text!

burnt_hand