By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 1,045 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

How to stop Symbols being converted

P: n/a
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

Nov 24 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
actually, ignore this, i've just answered my own question - it's
because < are invalid xml chars...
jumblesale wrote:
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 (&lt;/ &gt;)
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
Nov 24 '06 #2

P: n/a
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
Nov 24 '06 #3

P: n/a
Hello,

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

--
Cheers,
Gary
http://www.garyshort.org/

jumblesale wrote:
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 (&lt;/ &gt;)
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
Nov 24 '06 #4

P: n/a
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
Nov 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.