Question

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>
Was it helpful?

Solution

It is not an XML comment, but it may very well be a comment for the application that processes the content of the XML.

OTHER TIPS

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:

 &#42;

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("*", "&#42;") 

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

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 -->

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top