XamlStyler icon indicating copy to clipboard operation
XamlStyler copied to clipboard

Formatting fails if a comment contains 'xmlns:'

Open CorneliusRupertJackson opened this issue 2 years ago • 2 comments

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:

  1. Create a new XAML file (ResourceDictionary in the example)
  2. 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>
  1. Select the right-click option "Format XAML"
  2. The error messagebox appears: image

Version Info (please complete the following information):

  • OS: Windows 10 Pro (21H2)
  • Visual Studio: VS2022 (17.5.5)
  • XAML Styler: Extension (3.2208.1)

CorneliusRupertJackson avatar May 15 '23 12:05 CorneliusRupertJackson

This looks like a bug in the XML parsing...🤔

mrlacey avatar Jan 10 '24 10:01 mrlacey

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

mrlacey avatar Jan 11 '24 08:01 mrlacey