XamlStyler
XamlStyler copied to clipboard
Formatting fails if a comment contains 'xmlns:'
Describe the bug The formatting fails with an error messagebox with the following content:
Error in StylerPackage:
'<', hexadecimal value 0x3C, is an invalid attribute character. Line 9, position 5.
This occurs when a comment that contains the string "xmlns:" is located above an XAML element anywhere.
The XAML element needs at least one set property (as in x:Name="Button" or Foreground="Black")
To Reproduce Steps to reproduce the behavior:
- Create a new XAML file (ResourceDictionary in the example)
- Add some XAML element and a comment containing 'xmlns:' that is located above the element
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Comment that contains 'xmlns:' -->
<Style x:Key="SomeStyle" />
</ResourceDictionary>
- Select the right-click option "Format XAML"
- The error messagebox appears:
Version Info (please complete the following information):
- OS: Windows 10 Pro (21H2)
- Visual Studio: VS2022 (17.5.5)
- XAML Styler: Extension (3.2208.1)
This looks like a bug in the XML parsing...🤔
This looks like a bug in the XML parsing...🤔
Nope. It's an issue with the XmlEscapingService not handling this correctly.
The "escaped" version of the above produces this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="[x]http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Comment that contains 'xmlns:' -->
<Style x:Key="[' -->
<Style x:Key]SomeStyle" />
</ResourceDictionary>
This in turn causes XDocument.Parse to fail. (because the escaped XML isn't valid.)