Escape double quote character in XML

前端 未结 8 1140
旧时难觅i
旧时难觅i 2020-11-29 07:55

Is there an escape character for a double quote in xml? I want to write a tag like:


but if I put \"

相关标签:
8条回答
  • 2020-11-29 08:19

    Try this:

    "
    
    0 讨论(0)
  • 2020-11-29 08:21

    In C++ you can use EscapeXML ATL API. This is the correct way of handling special chars ...

    0 讨论(0)
  • 2020-11-29 08:23

    No there isn't an escape character as such, instead you can use &quot; or even <![CDATA["]]> to represent the " character.

    0 讨论(0)
  • 2020-11-29 08:27

    Others have answered in terms of how to handle the specific escaping in this case.

    A broader answer is not to try to do it yourself. Use an XML API - there are plenty available for just about every modern programming platform in existence.

    XML APIs will handle things like this for you automatically, making it a lot harder to go wrong. Unless you're writing an XML API yourself, you should rarely need to worry about the details like this.

    0 讨论(0)
  • 2020-11-29 08:30

    You can try using the a backslash followed by a "u" and then the unicode value for the character, for example the unicode value of the double quote is

    " -> U+0022

    Therefore if you were setting it as part of text in XML in android it would look something like this,

    <TextView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:text=" \u0022 Showing double quotes \u0022 "/>
    

    This would produce a text in the TextView roughly something like this

    " Showing double quotes "

    You can find unicode of most symbols and characters here www.unicode-table.com/en

    0 讨论(0)
  • New, improved answer to an old, frequently asked question...

    When to escape double quote in XML

    Double quote (") may appear without escaping:

    • In XML textual content:

      <NoEscapeNeeded>He said, "Don't quote me."</NoEscapeNeeded>
      
    • In XML attributes delimited by single quotes ('):

      <NoEscapeNeeded name='Pete "Maverick" Mitchell'/>
      

      Note: switching to single quotes (') also requires no escaping:

      <NoEscapeNeeded name="Pete 'Maverick' Mitchell"/>
      

    Double quote (") must be escaped:

    • In XML attributes delimited by double quotes:

      <EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell"/>
      

    Bottom line

    Double quote (") must be escaped as &quot; in XML only in very limited contexts.

    0 讨论(0)
提交回复
热议问题