Jeffrey Cameron
Jeffrey Cameron

Reputation: 10285

Linq to XML - Trying to print the Infinity symbol

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?

Upvotes: 2

Views: 1198

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500465

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.

Upvotes: 5

Related Questions