C# 摘要注释中的 XML 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/607094/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:06:47  来源:igfitidea点击:

Xml string in a C# summary comment

c#documentation

提问by loomisjennifer

I'm documenting a few methods I wrote in C# that deal with parsing tokens. Due to some technical restraints in other areas of the system, these tokens need to take the form of XML elements (i.e., <tokenName />). I'd like to put the format of those tokens in the summary statement itself.

我正在记录我在 C# 中编写的一些处理解析令牌的方法。由于系统其他领域的一些技术限制,这些令牌需要采用 XML 元素(即<tokenName />)的形式。我想将这些标记的格式放在摘要语句本身中。

However, this throws an error: Badly formed XML -- A name was started with an invalid character". Is there any sort of escape character sequence I can use to embed XML in my C# summary comments?

但是,这会引发错误:格式错误的 XML -- 名称以无效字符开头”。是否可以使用任何类型的转义字符序列将 XML 嵌入到我的 C# 摘要注释中?

采纳答案by Andrew Arnott

Use standard XML escaping. For example:

使用标准的 XML 转义。例如:

<summary>This takes a &lt;token1&gt; and turns it into a &lt;token2&gt;</summary>

It's not super-easy to type or read as code, but IntelliSense properly unescapes this and you see the right, readable thing in the tooltip.

作为代码输入或阅读并不是非常容易,但 IntelliSense 正确地对此进行了转义,您会在工具提示中看到正确、可读的内容。

回答by Sean

Use a CDATA section. For example:

使用 CDATA 部分。例如:

<![CDATA[ <name>Bob</name> ]]>

This is more elegant and readable in source than encoding special characters in entity references when you have a larger XML piece.

当您拥有更大的 XML 片段时,这比在实体引用中编码特殊字符更优雅、更易读。

If the XML you want to embed itself contains CDATA sections, you need to use multiple CDATA sections as described in another answer on Stack Overflowor on Wikipedia. Or you can always use plain entity references as described in other answers here.

如果要嵌入的 XML 本身包含 CDATA 部分,则需要使用多个 CDATA 部分,如Stack OverflowWikipedia上的另一个答案中所述。或者,您始终可以使用此处其他答案中所述的普通实体引用。

回答by FloWi

I use escape-sequences, because VisualStudios tooltip doesn't display anything that's inside a CDATA-section.

我使用转义序列,因为 VisualStudios 工具提示不显示 CDATA 部分内的任何内容。

回答by Mario

It's very late, but ran into the same problem, using <![CDATA[]]>will hide the comment in Intellisense.

很晚了,但遇到了同样的问题,使用<![CDATA[]]>将隐藏智能感知中的评论。

Replacing both <and >was to much work for me (lazy :) ). I found out that just replacing the <with &lt;was enough for the Intellisense because it makes the xml invalid and suitable for the Intellisense to parse as text in your summary block.

更换两个<>是为我的许多工作(懒惰:))。我发现对于 Intellisense仅替换<with&lt;就足够了,因为它使 xml 无效并且适合 Intellisense 将其解析为摘要块中的文本。

Here is an example:

下面是一个例子:

/// <summary>
/// Parse the queue process response
/// <para>&lt;?xml version="1.0" encoding="utf-16"?>&lt;result success="True">&lt;entity type="resource" operation="update" />&lt;/result></para>
/// <![CDATA[
/// <?xml version="1.0" encoding="utf-16"?><result success="True"><entity type="resource" operation="update" /></result>
/// ]]></summary>
/// <param name="response"></param>
/// <returns></returns>

The Intellisense will show this:

智能感知将显示:

Parse the queue process response
<?xml version="1.0" encoding="utf-16"?><result success="True"><entity type="resource" operation="update" /></result>