views:

1114

answers:

2

I have to submit a HTML form to a 3rd party website and one of the hidden fields is an XML string. The XML needs escaping before it is sent to the 3rd party.

However when I add the plain XML to the form field it semi-escapes it for me. So then when I use HTMLEncode myself part of the XML is double-escaped. How do I prevent the automatic escaping that appears to becoming from .NET.

Or even better how else can send the escaped XML via the hidden field.

XML

<systemCode>APP</systemCode>

Basic assigning to hidden input field

&lt;systemCode>APP&lt;/systemCode>

When I HTML Encode it as well

&amp;lt;systemCode&amp;gt;APP&amp;lt;/systemCode&amp;gt;

I can see what's happening - but I don't know how to prevent it?

Thanks

+2  A: 

Don't use HTMLEncode as well ! Use it alone !

Something like:

'Setting value:
hdnField.Value = Server.HtmlEncode("<systemCode>APP</systemCode>")
'Outputs: &amp;lt;systemCode&amp;gt;APP&amp;lt;/systemCode&amp;gt;

'Retrieving encoded value:
Dim escaped as string = Request.Form("hdnField")
'Retrieves: &lt;systemCode&gt;APP&lt;/systemCode&gt;

'Retrieving decoded value:
Dim myValue As String = Server.HtmlDecode(Request.Form("hdnField"))
'Retrieves: "<systemCode>APP</systemCode>"
Cerebrus
Say it one more time .... but this time with feeling ! ;)
Chad Grant
I will if you will vote one more time... this time with feeling ! :P
Cerebrus
Hi, I'm not using HTMLEncode as well as anything else - I AM using it alone but as I said there is something else going on automatically that is encoding the < of the XML from just this: portalReq.Value = myXML - it's the automatic stuff I want to stop!
David A Gibson
You cannot stop the "automatic stuff". ASP.NET will make sure that your rendered markup is valid XHTML. The only option is to assign the field's value to an HTMLEncoded string as I have shown above.
Cerebrus
I don't have control over the decoding I am sending this to a 3rd party. They are complaining that it is double encoded - so your option doesn't work for this specific case although I understand what you mean.
David A Gibson
A: 

In the end I used a literal and then HTMLEncoding the XML string before assigned a HTML form variable to the literal text field. A little bit like below:

portalReq.Text = "<input type=""hidden"" name=""portalReq"" value='" & HTMLENCODE(RequestXML) & "' />"

Not elegant but it's circumventing the problem.

David A Gibson