How to stop Symbols being converted

J

jumblesale

Hello,
I have a console app that converts fields from a database into xml
files and am having problems with 'Greater Than' and 'Less Than'
symbols being converted into their html equivalents (</ >)
automatically. I need them to stay as < > so that HTML controls render
correctly when the XML file is XSLT transformed in the consuming web
app.

At the moment, I am building the XML file like this:

MemoryStream msQuestion = new MemoryStream();
StreamReader myReader = new StreamReader(msQuestion);
XmlTextWriter xtwQuestion = new XmlTextWriter(msQuestion,
Encoding.UTF8);
string strQuestion = "";
DataRow myBody = dsQA.Tables[0].Rows[0];
DataRow myRow;

xtwQuestion.WriteStartDocument();
xtwQuestion.WriteStartElement("questions");
xtwQuestion.WriteElementString("question",
myBody["question"].ToString());
....
xtwQuestion.WriteEndElement();//question
xtwQuestion.WriteEndDocument();
xtwQuestion.Flush();

msQuestion.Position = 0;
strQuestion = myReader.ReadToEnd();
msQuestion.Flush();

The problem is that when inspected, the string 'strQuestion' contains
&lt;/ &gt; instead of < >

I've checked and the database value contains < >, so the conversion
must be done automatically somewhere.

Is there anyway to stop this conversion and leave the symbols as they
are?

Thanks for your time,
J
 
J

jumblesale

actually, ignore this, i've just answered my own question - it's
because < > are invalid xml chars...
 
M

Marc Gravell

Well, you don't make it clear what the context is; if a typical question is
"is 5 < 4", then this is the *correct* handling, and &lt; will render
correctly as < in the browser; if a typical question is itself HTML / XML,
then you could append the literal (WriteRaw()), but be aware that this does
risk injection attacks if someboody can get <SCRIPT/> or <OBJECT/> or
<IFRAME/> etc tags into the questions.

So what is a typcial question?

Marc
 
G

gary

Hello,

Does your database not support select as XML? Also, a DataSet will
provide you with xml.
 
M

Marc Gravell

No - < and > are definitely valid xml characters, but they need to be
escaped if you mean "a string with a less-than sign".

Note that CDATA can also be of use in wrapping malformed strings, but in
your case I think it is already doing the right thing. XSLT and browsers
will both know what the &lt; means.

Marc
 

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