views:

145

answers:

1

Hey all,

I am using Linq to XML for some HTML output files. I need to place the infinity symbol (∞) in the code on output in some table cells. I am creating an XElement like this

var table = new XElement("table",
            new XElement("tr",
                new XElement("td", "∞")
            )
        );

var document = new XDocument(table); document.Save(myFile);

and when the file is saved I am not seeing ∞, instead I see &#8734. How do I prevent this translation from happening?

+4  A: 

LINQ to XML is doing the right thing - it's assuming that when you give it a string as content, that's the content you want to see. It's doing escaping for you. You really don't want to have to escape every <, > and & yourself.

What you need to do is give it the actual content you want - which is the infinity symbol. So try this:

var table = new XElement("table",
            new XElement("tr",
                new XElement("td", "\u8734")
            )
        );

That may well end up not coming out as an entity in the output file, just the encoded character - but that should be okay, so long as you don't have encoding issues.

EDIT: I've just checked, and the infinity symbol is actually U+221E, so you want "\u221e" instead. I can't actually see what U+8734 is meant to be... it may not be defined in Unicode at the moment.

Jon Skeet
For what it's worth, in LINQPad, instead of an infinity symbol, I see a Chinese character that may or may not come through when I paste it here: 蜴
Joel Mueller
@Joel: Yup, I've just checked and infinity is U+221E. Will edit.
Jon Skeet
Thanks, I will try this today and let you know how it goes.
Jeffrey Cameron
It worked beautifully, thanks!
Jeffrey Cameron