473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Encode(quote) attribute value

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
5 13216
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matthew Simoneau | last post by:
I'm trying to figure out how to HTML encode shift_jis text and put it into an attribute. Starting with this XML-file with characters encoded in shift_jis <?xml version="1.0"...
4
by: Newbie | last post by:
How would I modify this form to encode *all* the characters in the 'source' textarea to the '%xx' format & place result code into the 'output' textarea? (cross browser compatable) Any help is...
3
by: QQ | last post by:
Basically i have a database record field that stores long string. <Field Name = "Data", value="xxx"/Field> xxx could be very long. Is there anyway that I can url encode it or reference the...
4
by: Paul Hadfield | last post by:
Hi, Wonder if anyone can help me on this, In DotNet2.0 I've been quite happily using the WriteAttributeString method of XmlWriter object, but have run into a problem when trying to output a...
1
by: Anil G | last post by:
Hi! If page has the link, let say page has following hyperlink <a href="javascript:editUser('<c:out value="${users.id}"/>')"><c:out value="${users.username}"/></a> Now, on web browser, bottom...
3
by: rameshkumarc | last post by:
i m decoding a string using jsp (ex:encode value="hello+world")...when i m decoding it;s coming as "hello world"...it's working fine in jsp when i m putting the value in html text field only the...
7
by: erikcw | last post by:
Hi, I'm trying to build a SQL string sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""", (cid, ag, self.data) It raises this error: AttributeError: 'tuple' object has no...
0
by: louvino | last post by:
I send tue querystring : ?lib=l%27%C3%A9cole that means "l'école". I wish I created a anchor : <a href='test.aspx?id=< %=HttpUtility.UrlEncode(Request.QueryString)%>'>click here</a> But I...
4
by: J Peyret | last post by:
Well, as usual I am confused by unicode encoding errors. I have a string with problematic characters in it which I'd like to put into a postgresql table. That results in a postgresql error so I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.