Ampersand in value gives me a pain

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RD

    Ampersand in value gives me a pain

    Some of the customers in the table I'm trying to see in an XML doc have an
    ampersand in the data. Looking at them and formatting the XML doc with a
    style sheet this generates an error. I'm creating the XML doc with VB.Net
    code, simply writing extracted values from my database to the XML doc. What
    do I have to do so that the ampersands in the data does not create problems
    while viewing the XML?

    Any help would b egreatly appreciated.

    Bob.


  • Bruce Wood

    #2
    Re: Ampersand in value gives me a pain

    You have to write an EscapeForXml() function in your VB program that
    accepts a string from your database and returns a string ready to
    insert into XML.

    Your function should make the following transformations to whatever
    string you pass it:

    The '<' character should be written out as &lt; -- that's ampersand,
    el, tee, semicolon.
    The '>' character should be written out as &gt;
    The double-quote character " should be written out as &quot;
    The single-quote character ' should be written out as &apos;
    The ampersand character & should be written out as &amp;

    Every other printable ASCII character can be left as it is. If your
    database strings contain control characters or characters about value
    127 then you may want to filter them out. Otherwise you have to specify
    the character set in your XML header.

    (In fact you don't _really_ need to escape the single quote character
    to &apos;, but it doesn't hurt.)

    Comment

    • rdcpro

      #3
      Re: Ampersand in value gives me a pain

      You could also enclose the data in the element in a CDATA section:

      <foo><![CDATA[me & you]]></foo>

      There are some utility methods, such as HTMLEncode() but you need an HTTP
      context for this, and the OP hasn't said what the environment is.

      Regards,
      Mike Sharp


      "Bruce Wood" wrote:
      [color=blue]
      > You have to write an EscapeForXml() function in your VB program that
      > accepts a string from your database and returns a string ready to
      > insert into XML.
      >
      > Your function should make the following transformations to whatever
      > string you pass it:
      >
      > The '<' character should be written out as < -- that's ampersand,
      > el, tee, semicolon.
      > The '>' character should be written out as >
      > The double-quote character " should be written out as "
      > The single-quote character ' should be written out as &apos;
      > The ampersand character & should be written out as &
      >
      > Every other printable ASCII character can be left as it is. If your
      > database strings contain control characters or characters about value
      > 127 then you may want to filter them out. Otherwise you have to specify
      > the character set in your XML header.
      >
      > (In fact you don't _really_ need to escape the single quote character
      > to &apos;, but it doesn't hurt.)
      >
      >[/color]

      Comment

      Working...