XML中的双星号注释符号



在XML中使用双星号表示行注释是否有效?我在分配给我解析的XML文件中经常看到这种情况。

的例子:

</someClosingTag>
** This is my line comment in an XML file.
<someOpeningTag>

在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:devbinMyApp.aspx.cs:464
MyApp.CreateDictionaryFromXml(String Xml, Int32& Version) in c:devbinMyApp.aspx.cs:447
MyApp.GetEmailFromXml(String Xml) in c:devbinMyApp.aspx.cs:402
MyApp.btnSubmit_Click(Object sender, EventArgs e) in c:devbinMyApp.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

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

解决方法很简单——只需将任何星号实例替换为:

 &#42;

根据XML标准,这是合法的,System.Xml.XmlReader没有问题。如果您正在使用xmltextwwriter生成XML,那么您必须在事后对此进行处理;可能

Xml = Xml.Replace("*", "&#42;") 

就可以了,因为我不认为星号会在XML中用于任何其他目的。

它不是XML注释,但它很可能是处理XML内容的应用程序的注释。

这不是用XML编写注释的正确方式,您可以在这里阅读:http://en.wikipedia.org/wiki/XML#Comments

一个有效注释的例子:<!-- no need to escape <code> & such in comments -->

最新更新