Output XML non-breaking space (#160) from code

J

James Thurley

I have an XmlDocument object, and need to output the   entity from
my code. I potentially need to output other similar entities such as
£ for the GBP sign.

Here is some example code:

//-----------------
XmlDocument doc = new XmlDocument();
XmlElement testElement = doc.CreateElement("test");
doc.AppendChild(testElement);

XmlNode spaceNode = doc.CreateEntityReference("160");

testElement.AppendChild(spaceNode);
StringWriter sw = new StringWriter();
XmlWriter xw = new XmlTextWriter(sw);
doc.WriteContentTo(xw);
//-----------------

So far I have tried:

// Produces " "
doc.CreateTextNode(" ");

// Produces " 
doc.CreateTextNode(" ");

// Exception: "Cannot create an EntityReference node with a name
// starting with '#'."
doc.CreateEntityReference("#160");

// Exception: "There is an invalid name character in 160"
doc.CreateEntityReference("160");

It's frustratingly hard just to put " " into my XML document...
Have I missed something obvious?

Thanks,
James.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top