如何在 XML 代码中使用“&”字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7823813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Can I use '&' character in XML code?
提问by Balázs Erd?s
I want to use & character but the Visual Studio throw exception. How Have to write this?
我想使用 & 字符,但 Visual Studio 抛出异常。这个怎么写?
回答by Birey
Replace any &with
将任何替换&为
&
It would load properly in XML.
它会在 XML 中正确加载。
回答by James Johnson
You can use &or &, or you can wrap it in a CDATAsection like this:
您可以使用&或&,也可以将其包装在这样的CDATA部分中:
<![CDATA[Foo & Bar]]>
回答by Quentin
There are two ways to represent characters which have special meaning in XML (such as <and >) in an XML document.
有两种方法可以在 XML 文档中表示在 XML 中具有特殊含义的字符(例如<和>)。
- CDATA sections
- As entities
- CDATA 部分
- 作为实体
A CDATA section can only be used in places where you could have a text node.
CDATA 部分只能用在可以有文本节点的地方。
<foo><![CDATA[Here is some data including < and > (and &!) ]]></foo>
The caveat is that you can't include the sequence ]]>as data in a CDATA section.
需要注意的是,您不能将序列]]>作为数据包含在 CDATA 部分中。
Entities can be used everywhere (except inside CDATA sections) and consist of &, then an identifier, then ;.
实体可以在任何地方使用(除了在 CDATA 部分内)并且由&、标识符和;.
<foo>Here is some data including < and > (and &!)</foo>
回答by Dave Newton
Use the entity, &.
使用实体,&。
(+1 to the other answers that discuss CDATA.)
(对讨论 CDATA 的其他答案+1。)
回答by COD3BOY
You may use a CDATA:
Everything inside a CDATAsection is ignored by the parser.
您可以使用CDATA:CDATA解析器忽略部分内的所有内容。
A CDATA section starts with <![CDATA[and ends with ]]>
CDATA 部分<![CDATA[以]]>

