XML 中的双星号注释表示法
在 XML 中使用双星号来指示行注释是否有效?我在分配给我解析的 XML 文件中经常看到这种情况。
例子:
</someClosingTag>
** This is my line comment in an XML file.
<someOpeningTag>
Is it valid to use a double-asterisk to indicate a line-comment in XML? I see this a lot in the XML files I've been assigned to parse.
Example:
</someClosingTag>
** This is my line comment in an XML file.
<someOpeningTag>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XML 标准中没有任何内容( http://www.w3.org/TR/xml/ )关于 ** 作为有效注释(正确的符号是:
)。
但是,我编写了一个 C#.Net 程序来使用 .Net 2.0 System.Xml.XmlReader 类来解析 XML,但它因我拥有的 ** 值而卡住了。我没有将其用作评论;这是一个合法的字符串值。经过进一步的实验,我发现它会被任何星号字符阻塞,甚至是一个星号字符,如下所示:
生成的异常是:
这里有两个奇怪的东西。首先,当我使用 XmlTextWriter 生成 XML 时,星号没有以任何方式分隔(但 XmlReader 无法将其读回)。其次,在谷歌搜索之后,我在网络上找不到其他人遇到这个问题。我无法想象为什么这么多年过去了,没有人会在 XML 值中使用星号,从而不报告这一点。
解决方法非常简单 - 只需将星号的任何实例替换为:
根据 XML 标准,这是合法的,并且 System.Xml.XmlReader 对此没有任何问题。如果您使用 XmlTextWriter 生成 XML,那么您必须在事后为此做一个创可贴;可能
没问题,因为我认为星号不会在 XML 中用于任何其他目的。
There is nothing in the XML standard ( http://www.w3.org/TR/xml/ ) about ** as a valid comment (the correct notation is:
).
However, I wrote a C#.Net program to parse XML using the .Net 2.0 System.Xml.XmlReader class, and it choked on a ** value that I had. I was not using it as a comment; it was a legitimate string value. After doing further experimentation, I found that it choked on ANY asterisk character, even a single one, like this:
The exception generated was:
Two weird things here. First, when I use XmlTextWriter to generate XML, the asterisk is not delimited in any way (yet XmlReader can't read it back in). Second, after Googling, I can't find anyone else on the web having this problem. I can't imagine why, after all these years, no one would ever use an asterisk in an XML value and thus not report this.
The workaround is quite simple - just replace any instance of an asterisk with:
This is legal according to the XML standard and System.Xml.XmlReader has no problems with it. If you're using XmlTextWriter to generate XML, you'll have to do a band-aid for this after the fact; probably
would be OK as I don't think asterisks would be used for any other purpose in XML.
它不是 XML 注释,但很可能是处理 XML 内容的应用程序的注释。
It is not an XML comment, but it may very well be a comment for the application that processes the content of the XML.
这不是在 XML 中编写注释的正确方法,您可以在此处阅读:http://en .wikipedia.org/wiki/XML#Comments
This is not the right way to write a comment in XML as you can read here: http://en.wikipedia.org/wiki/XML#Comments