99

I used this syntax as I found online but it throws an error:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<!-- Cool comment -->
xmlns:System="clr-namespace:System;assembly=mscorlib"

'Name cannot begin with the '<' character, hexadecimal value 0x3C. Line 4, position 5.' XML is not valid.

6 Answers 6

113

I assume those XAML namespace declarations are in the parent tag of your control? You can't put comments inside of another tag. Other than that, the syntax you're using is correct.

<UserControl xmlns="...">
    <!-- Here's a valid comment. Notice it's outside the <UserControl> tag's braces -->
    [..snip..]
</UserControl>
4
  • 2
    Thanks so where can I put it? I need to put them in certain places that shows my additions to code.
    – Joan Venge
    Commented Oct 27, 2011 at 20:06
  • As per my example, you'll need to put them adjacent to the tag whose contents you've changed. You won't be able to literally put them beside an attribute that you add inside a tag. Is this required for something that's going to automatically parse the XAML and do something based on your comments, or just for other people to see what you've changed?
    – Dan J
    Commented Oct 27, 2011 at 20:07
  • 1
    You put them on the outside of XML tags. <sometag <!-- not here --> /> . If you go back to your code, you'll see that the place you put them receiving the error message is within a multi-line tag.
    – Tormod
    Commented Oct 27, 2011 at 20:09
  • Although this answer suits me best, User500099 gave the exact answer to Joan's question.
    – Hacky
    Commented Sep 1, 2021 at 7:18
38

Found a nice solution by Laurent Bugnion, it can look something like this:

<UserControl xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:comment="Tag to add comments"
             mc:Ignorable="d comment" d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Button Width="100"
                comment:Width="example comment on Width, will be ignored......">
        </Button>
    </Grid>
</UserControl>

Here's the link: http://blog.galasoft.ch/posts/2010/02/quick-tip-commenting-out-properties-in-xaml/

A commenter on the link provided extra characters for the ignore prefix in lieu of highlighting:

mc:Ignorable=”ØignoreØ”
3
  • 4
    Since XAML is a specialization XML (which is a specialization of SGML), you would think the -- SGML comment -- style would work for inside tag comments. But no, 99.44% of XAML parsers do not accept SGML in-tag comments. Commented Jun 19, 2012 at 18:43
  • This is the most useful answer!
    – J F
    Commented Jul 9, 2012 at 12:25
  • 3
    I think the question by @Joan Venge is how to "Remove code temporarily" rather than "Adding information for human readers".
    – Lei Yang
    Commented Feb 23, 2014 at 1:25
34

You can't insert comments inside xml tags.

Bad

<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<!-- Cool comment -->
xmlns:System="clr-namespace:System;assembly=mscorlib">

Good

<Window xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<!-- Cool comment -->
5
  • 9
    @kenny: That comment is OT and ranty... Basing any programming language off XML sort of sucks, but at least XAML had the good sense to try to make everything as declarative as absolutely possible, which fits with the XML model. Commented Oct 27, 2011 at 20:21
  • 3
    @kenny a: xaml isn't a programming language - it is a markup language and/or serialization format, depending on how you look at it... and b: that limitation is not one of xaml - it is one of xml. Commented Nov 16, 2011 at 10:41
  • 6
    I understand XAML's limitation is that it's based off XML. So, XML is a second rate programming/markup language. Is that better?
    – kenny
    Commented Nov 16, 2011 at 17:07
  • 1
    >XML is a programming language ...what?
    – rr-
    Commented Mar 13, 2015 at 10:50
  • 1
    Someday, the world will stop referring to HTML/XML/et al as programming languages. Not today.
    – rw-nandemo
    Commented Dec 31, 2015 at 21:27
24

Just a tip:

In Visual Studio to comment a text, you can highlight the text you want to comment, and then use Ctrl + K followed by Ctrl + C. To uncomment, you can use Ctrl + K followed by Ctrl + U.

3
  • 1
    Using this all the time when programming as usual but when I entered the XAML designer I didn't think of this one. Works fine. Commented Jul 28, 2016 at 8:26
  • 1
    this works only when you are outside of a tag definition. "<x><!--comment-->content</x>" works, "<x <!--comment--> > content</x>" won't. I tried to comment out some properties on multi lines, it gave error. Commented May 1, 2018 at 19:39
  • Just for clarity, in Visual Studio 201 this refers to the Edit.CommentSelection and Edit.UncommentSelection commands.
    – AlainD
    Commented Nov 19, 2020 at 14:46
0

You cannot put comments inside UWP XAML tags. Your syntax is right.

TO DO:

<xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"/>
<!-- Cool comment -->

NOT TO DO:

<xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    <!-- Cool comment -->
xmlns:System="clr-namespace:System;assembly=mscorlib"/>
-1

For anyone learning this stuff, comments are more important, so drawing on Xak Tacit's idea
(from User500099's link) for Single Property comments, add this to the top of the XAML code block:

<!--Comments Allowed With Markup Compatibility (mc) In XAML!
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ØignoreØ="http://www.galasoft.ch/ignore"
    mc:Ignorable="ØignoreØ"
    Usage in property:
ØignoreØ:AttributeToIgnore="Text Of AttributeToIgnore"-->

Then in the code block

<Application FooApp:Class="Foo.App"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ØignoreØ="http://www.galasoft.ch/ignore"
mc:Ignorable="ØignoreØ"
...

AttributeNotToIgnore="TextNotToIgnore"
...

...
ØignoreØ:IgnoreThisAttribute="IgnoreThatText"
...   
>
</Application>

Not the answer you're looking for? Browse other questions tagged or ask your own question.