422,530 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,530 IT Pros & Developers. It's quick & easy.

Encode(quote) attribute value

P: n/a
Hi there,

I need to encode the value of an XML attribute, e.g.

string myXml = "<element myAttr=\"" +
encodeURIComponent("my \"value") + "\"" ....";

Is there an equivalent of encodeURIComponent() javascript function as
part of the framework (1.1 or 2.0) which will encode special
characters, e.g. double quotes?

Thanks,
-Oleg.

Oct 19 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
For .NET languages, include the namespace System.Web.HttpServerUtility
and call the HtmlEncode method of the Server object. The results are
equivalent to the javascript escape() method.

ie:

for VB.NET, where re

Imports System.Web.HttpServerUtility 'javascript escape/unescape
equivalency

encodedText = Server.HtmlEncode(unencodedText)

Andy

Oct 19 '06 #2

P: n/a
Andy wrote:
For .NET languages, include the namespace System.Web.HttpServerUtility
and call the HtmlEncode method of the Server object. The results are
equivalent to the javascript escape() method.
Where did you get that idea? HtmlEncode and JavaScript escape are
different in the results they deliver. For instance HtmlEncode escapes
'<' as '&lt;' while escape escapes '<' as '%3C'.

And the original poster asked about encodeURIComponent and not escape.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 20 '06 #3

P: n/a
Ol*********@gmail.com wrote:
I need to encode the value of an XML attribute, e.g.

string myXml = "<element myAttr=\"" +
encodeURIComponent("my \"value") + "\"" ....";

Is there an equivalent of encodeURIComponent() javascript function as
part of the framework (1.1 or 2.0) which will encode special
characters, e.g. double quotes?
JScript.NET is part of the .NET framework so one solution with .NET is
to reference the JScript.NET dll for the global object and use its
encodeURIComponent method.

But .NET independent of JScript.NET in its API has methods to encode
URLs, see the UrlEncode method in the HttpUtility class
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebHttpUtilityClassUrlEncodeTopic.asp>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 20 '06 #4

P: n/a

Actually, I'm not looking for encodeURIComponent() per se. I'm looking
for a function that will properly escape an XML attribute value in
cases when I'm creating XML document manually, i.e. by concatenating
strings rather than using XmlDocument/XmlAttribute/etc classes.

In this example I provided, the value of the attribute has a double
quote character ("), e.g. "my "value", so it needs to be replaced with
"my &quot;value". Is there a function that does that? If not, what
other characters do I need to replace?

-Oleg.

Martin Honnen wrote:
Ol*********@gmail.com wrote:
I need to encode the value of an XML attribute, e.g.

string myXml = "<element myAttr=\"" +
encodeURIComponent("my \"value") + "\"" ....";

Is there an equivalent of encodeURIComponent() javascript function as
part of the framework (1.1 or 2.0) which will encode special
characters, e.g. double quotes?

JScript.NET is part of the .NET framework so one solution with .NET is
to reference the JScript.NET dll for the global object and use its
encodeURIComponent method.

But .NET independent of JScript.NET in its API has methods to encode
URLs, see the UrlEncode method in the HttpUtility class
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebHttpUtilityClassUrlEncodeTopic.asp>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 20 '06 #5

P: n/a
Ol*********@gmail.com wrote:
In this example I provided, the value of the attribute has a double
quote character ("), e.g. "my "value", so it needs to be replaced with
"my &quot;value". Is there a function that does that? If not, what
other characters do I need to replace?
If you use XmlTextWriter (respective XmlWriter in .NET 2.0) to construct
your XML then it does all escaping automatically for you e.g. in the
WriteAttributeString call e.g. C#

xmlWriter.WriteStartElement("root");
xmlWriter.WriteAttributeString("attribute", @"Kibo is ""GOD""");

will yield the XML markup

<root attribute="Kibo is &quot;GOD&quot;" />

So all you need to use is the proper XML APIs (e.g. XmlWriter) and
escaping to meet XML well-formedness rules is done automatically.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.