XML 中的双星号注释表示法

发布于 2024-12-14 20:29:34 字数 184 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看透却不说透 2024-12-21 20:29:34

XML 标准中没有任何内容( http://www.w3.org/TR/xml/ )关于 ** 作为有效注释(正确的符号是:

<!-- My comment -->

)。

但是,我编写了一个 C#.Net 程序来使用 .Net 2.0 System.Xml.XmlReader 类来解析 XML,但它因我拥有的 ** 值而卡住了。我没有将其用作评论;这是一个合法的字符串值。经过进一步的实验,我发现它会被任何星号字符阻塞,甚至是一个星号字符,如下所示:

<?xml version="1.0" standalone="yes"?><HVACRJob Version="32">5 * 3 = 15</HVACRJob>

生成的异常是:

[XmlException: Unexpected end of file has occurred. The following elements are not closed: HVACRJob. Line 1, position 25.]
System.Xml.XmlTextReaderImpl.Throw(Exception e) +95
System.Xml.XmlTextReaderImpl.ThrowUnclosedElements() +354
System.Xml.XmlTextReaderImpl.ParseElementContent() +5088529
System.Xml.XmlReader.ReadToFollowing(String name) +92
MyApp.ParseXmlBytesIntoDictionary(Dictionary`2 Dict, Byte[] ItemAsUncompressedXmlBytes, Boolean AllowOverwrite) in c:\dev\bin\MyApp.aspx.cs:464
MyApp.CreateDictionaryFromXml(String Xml, Int32& Version) in c:\dev\bin\MyApp.aspx.cs:447
MyApp.GetEmailFromXml(String Xml) in c:\dev\bin\MyApp.aspx.cs:402
MyApp.btnSubmit_Click(Object sender, EventArgs e) in c:\dev\bin\MyApp.aspx.cs:2155
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

这里有两个奇怪的东西。首先,当我使用 XmlTextWriter 生成 XML 时,星号没有以任何方式分隔(但 XmlReader 无法将其读回)。其次,在谷歌搜索之后,我在网络上找不到其他人遇到这个问题。我无法想象为什么这么多年过去了,没有人会在 XML 值中使用星号,从而不报告这一点。

解决方法非常简单 - 只需将星号的任何实例替换为:

 *

根据 XML 标准,这是合法的,并且 System.Xml.XmlReader 对此没有任何问题。如果您使用 XmlTextWriter 生成 XML,那么您必须在事后为此做一个创可贴;可能

Xml = Xml.Replace("*", "*") 

没问题,因为我认为星号不会在 XML 中用于任何其他目的。

There is nothing in the XML standard ( http://www.w3.org/TR/xml/ ) about ** as a valid comment (the correct notation is:

<!-- My comment -->

).

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:

<?xml version="1.0" standalone="yes"?><HVACRJob Version="32">5 * 3 = 15</HVACRJob>

The exception generated was:

[XmlException: Unexpected end of file has occurred. The following elements are not closed: HVACRJob. Line 1, position 25.]
System.Xml.XmlTextReaderImpl.Throw(Exception e) +95
System.Xml.XmlTextReaderImpl.ThrowUnclosedElements() +354
System.Xml.XmlTextReaderImpl.ParseElementContent() +5088529
System.Xml.XmlReader.ReadToFollowing(String name) +92
MyApp.ParseXmlBytesIntoDictionary(Dictionary`2 Dict, Byte[] ItemAsUncompressedXmlBytes, Boolean AllowOverwrite) in c:\dev\bin\MyApp.aspx.cs:464
MyApp.CreateDictionaryFromXml(String Xml, Int32& Version) in c:\dev\bin\MyApp.aspx.cs:447
MyApp.GetEmailFromXml(String Xml) in c:\dev\bin\MyApp.aspx.cs:402
MyApp.btnSubmit_Click(Object sender, EventArgs e) in c:\dev\bin\MyApp.aspx.cs:2155
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

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

Xml = Xml.Replace("*", "*") 

would be OK as I don't think asterisks would be used for any other purpose in XML.

不必你懂 2024-12-21 20:29:34

它不是 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.

攒一口袋星星 2024-12-21 20:29:34

这不是在 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

An example of a valid comment: <!-- no need to escape <code> & such in comments -->

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文