2

I have a problem loading an xml document contains comments like this:

<!--- value --->
    <item attribute = "somevalue"/>

I get this error with both XDocument.Load() and XmlDocument.LoadXml() methods : An XML comment cannot contain '--', and '-' cannot be the last character.

But XML specs on MSDN have an example with comments like this: http://msdn.microsoft.com/en-us/library/ms256201(v=vs.90).aspx

What's wrong in my xml syntax? Is it important to end the comment with "-->" sequence, or what? Can I exclude comment while loading XML?

Semant1ka
  • 647
  • 10
  • 26
  • Yes I think, comments can be ignored prior to loading XML. But have you checked your XML document is well-formed after using `--->` instead of `-->` to close the comments? – Hassan Jun 03 '14 at 07:38
  • No it doesn't. It has an example like this: (3 beginning hyphens, and 2 ending hyphens), which is valid, yours contains ---> (3 ending hyphens), which is not. "--" is not allowed in comments. – Willem van Rumpt Jun 03 '14 at 07:39

2 Answers2

2

This line <!--- value ---> says comment are not closed yet. Actually -- not permitted within XML comment.

Comment must end with this -->tag.

Validate your XML here or here.

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<!-- comment in -->
<note>
  <to>Tove</to>
  <from>Jani</from> 
  <heading>Reminder</heading>
  <!-- comment in -->
  <body>Don't forget me this weekend!</body>
  <!--- comment in --->
</note>

Validation Result:

enter image description here

For ignoring comments prior to loading XML you can see selected answer at following link:

How to ignore comments when reading a XML file into a XmlDocument?.

Community
  • 1
  • 1
Hassan
  • 5,360
  • 2
  • 22
  • 35
0

Yes, there's a xml parser error

Try this: using String.Replace

xmlString.Replace("---", "--");
Smilla J.
  • 1,124
  • 10
  • 15