Browse Source

Fix #1688: resolve elements to the XML namespace the document declares

An assembly can map one CLR namespace to several XML namespaces: PresentationFramework
maps its namespaces to both the winfx/2006 and the netfx/2007 presentation namespace. The
fallback used when the BAML xmlns records name no namespace for a type always preferred the
winfx/2006 one, so a document that binds the default prefix to netfx/2007 ended up with a
root start tag that declares one presentation namespace and needs the other for its own
name, which XmlWriter rejects. Only the root carries the xmlns declaration, which is why
skipping XClassRewritePass worked around it.

Assisted-by: Claude:claude-opus-5:Claude Code
pull/4096/head
Siegfried Pammer 2 weeks ago
parent
commit
f1bc6ab542
  1. 2
      ICSharpCode.BamlDecompiler/Xaml/XamlType.cs
  2. 25
      ICSharpCode.BamlDecompiler/XamlContext.cs
  3. 6
      ILSpy.BamlDecompiler.Tests.Windows/BamlTestRunner.cs
  4. 4
      ILSpy.BamlDecompiler.Tests.Windows/Cases/Issue1688.xaml
  5. 42
      ILSpy.BamlDecompiler.Tests.Windows/Cases/Issue1688.xaml.cs
  6. 6
      ILSpy.BamlDecompiler.Tests.Windows/ILSpy.BamlDecompiler.Tests.Windows.csproj
  7. 1
      ILSpy.BamlDecompiler.Tests/ILSpy.BamlDecompiler.Tests.csproj
  8. 115
      ILSpy.BamlDecompiler.Tests/XmlNamespaceResolutionTests.cs

2
ICSharpCode.BamlDecompiler/Xaml/XamlType.cs

@ -69,7 +69,7 @@ namespace ICSharpCode.BamlDecompiler.Xaml
xmlNs = ctx.XmlNs.LookupXmlns(FullAssemblyName, TypeNamespace); xmlNs = ctx.XmlNs.LookupXmlns(FullAssemblyName, TypeNamespace);
// Sometimes there's no reference to System.Xaml even if x:Type is used // Sometimes there's no reference to System.Xaml even if x:Type is used
if (xmlNs == null) if (xmlNs == null)
xmlNs = ctx.TryGetXmlNamespace(Assembly, TypeNamespace); xmlNs = XamlContext.TryGetXmlNamespace(Assembly, TypeNamespace, elem);
if (xmlNs == null) if (xmlNs == null)
{ {

25
ICSharpCode.BamlDecompiler/XamlContext.cs

@ -192,7 +192,7 @@ namespace ICSharpCode.BamlDecompiler
public const string KnownNamespace_Presentation = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; public const string KnownNamespace_Presentation = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
public const string KnownNamespace_PresentationOptions = "http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"; public const string KnownNamespace_PresentationOptions = "http://schemas.microsoft.com/winfx/2006/xaml/presentation/options";
public string TryGetXmlNamespace(IModule assembly, string typeNamespace) public static string TryGetXmlNamespace(IModule assembly, string typeNamespace, XElement context = null)
{ {
if (assembly == null) if (assembly == null)
return null; return null;
@ -214,12 +214,35 @@ namespace ICSharpCode.BamlDecompiler
possibleXmlNs.Add(xmlNs); possibleXmlNs.Add(xmlNs);
} }
// An assembly may map one CLR namespace to several XML namespaces; PresentationFramework
// for example maps its namespaces to both the winfx/2006 and the netfx/2007 presentation
// namespace. Whenever the document itself declares one of the candidates, that one has to
// win: picking a different candidate for an element whose start tag carries the xmlns
// declaration redefines the prefix within that tag, which is not valid XML.
var declared = possibleXmlNs.Where(ns => IsDeclaredIn(context, ns)).ToList();
if (declared.Count > 0)
possibleXmlNs = new HashSet<string>(declared);
if (possibleXmlNs.Contains(KnownNamespace_Presentation)) if (possibleXmlNs.Contains(KnownNamespace_Presentation))
return KnownNamespace_Presentation; return KnownNamespace_Presentation;
return possibleXmlNs.FirstOrDefault(); return possibleXmlNs.FirstOrDefault();
} }
static bool IsDeclaredIn(XElement context, string xmlNamespace)
{
for (var elem = context; elem != null; elem = elem.Parent)
{
foreach (var attr in elem.Attributes())
{
if (attr.IsNamespaceDeclaration && attr.Value == xmlNamespace)
return true;
}
}
return false;
}
public XName GetKnownNamespace(string name, string xmlNamespace, XElement context = null) public XName GetKnownNamespace(string name, string xmlNamespace, XElement context = null)
{ {
var xNs = GetXmlNamespace(xmlNamespace); var xNs = GetXmlNamespace(xmlNamespace);

6
ILSpy.BamlDecompiler.Tests.Windows/BamlTestRunner.cs

@ -135,6 +135,12 @@ namespace ILSpy.BamlDecompiler.Tests
RunTest("cases/issue1547"); RunTest("cases/issue1547");
} }
[Test]
public void Issue1688()
{
RunTest("cases/issue1688");
}
[Test] [Test]
public void Issue2052() public void Issue2052()
{ {

4
ILSpy.BamlDecompiler.Tests.Windows/Cases/Issue1688.xaml

@ -0,0 +1,4 @@
<ContextMenu x:Class="ILSpy.BamlDecompiler.Tests.Cases.Issue1688" xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cases="clr-namespace:ILSpy.BamlDecompiler.Tests.Cases">
<MenuItem Header="Assign Place" Click="Click_AssignPlace" />
<MenuItem Header="Assign Move" Click="Click_AssignMove" />
</ContextMenu>

42
ILSpy.BamlDecompiler.Tests.Windows/Cases/Issue1688.xaml.cs

@ -0,0 +1,42 @@
// Copyright (c) 2026 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System.Windows;
using System.Windows.Controls;
namespace ILSpy.BamlDecompiler.Tests.Cases
{
/// <summary>
/// Interaction logic for Issue1688.xaml
/// </summary>
public partial class Issue1688 : ContextMenu
{
public Issue1688()
{
InitializeComponent();
}
void Click_AssignPlace(object sender, RoutedEventArgs e)
{
}
void Click_AssignMove(object sender, RoutedEventArgs e)
{
}
}
}

6
ILSpy.BamlDecompiler.Tests.Windows/ILSpy.BamlDecompiler.Tests.Windows.csproj

@ -66,6 +66,9 @@
</Compile> </Compile>
<Compile Include="Cases\CustomControl.cs" /> <Compile Include="Cases\CustomControl.cs" />
<Compile Include="Cases\Issue1547.xaml.cs" /> <Compile Include="Cases\Issue1547.xaml.cs" />
<Compile Include="Cases\Issue1688.xaml.cs">
<DependentUpon>Issue1688.xaml</DependentUpon>
</Compile>
<Compile Include="Cases\Issue2097.xaml.cs" /> <Compile Include="Cases\Issue2097.xaml.cs" />
<Compile Include="Cases\Issue2116.xaml.cs" /> <Compile Include="Cases\Issue2116.xaml.cs" />
<Compile Include="Cases\Issue3318.xaml.cs" /> <Compile Include="Cases\Issue3318.xaml.cs" />
@ -102,6 +105,9 @@
<Page Include="Cases\Issue1547.xaml"> <Page Include="Cases\Issue1547.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="Cases\Issue1688.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Cases\Issue2052.xaml"> <Page Include="Cases\Issue2052.xaml">
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>

1
ILSpy.BamlDecompiler.Tests/ILSpy.BamlDecompiler.Tests.csproj

@ -44,6 +44,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="MissingReferencesTests.cs" /> <Compile Include="MissingReferencesTests.cs" />
<Compile Include="XmlNamespaceResolutionTests.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

115
ILSpy.BamlDecompiler.Tests/XmlNamespaceResolutionTests.cs

@ -0,0 +1,115 @@
// Copyright (c) 2026 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using System.Reflection.PortableExecutable;
using System.Xml.Linq;
using ICSharpCode.BamlDecompiler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using NUnit.Framework;
using ILSpy.BamlDecompiler.Tests;
// This assembly plays the role of an assembly that maps one CLR namespace to two XML namespaces,
// the way PresentationFramework maps its namespaces to both presentation namespaces.
[assembly: System.Windows.Markup.XmlnsDefinition(XmlNamespaceResolutionTests.Winfx2006Presentation, XmlNamespaceResolutionTests.TestClrNamespace)]
[assembly: System.Windows.Markup.XmlnsDefinition(XmlNamespaceResolutionTests.Netfx2007Presentation, XmlNamespaceResolutionTests.TestClrNamespace)]
namespace System.Windows.Markup
{
/// <summary>
/// Stand-in for the WPF attribute of the same name, which is unavailable on platforms without
/// WPF. The BAML decompiler matches it by full name in metadata, so the declaring assembly does
/// not matter.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
internal sealed class XmlnsDefinitionAttribute : Attribute
{
public XmlnsDefinitionAttribute(string xmlNamespace, string clrNamespace)
{
XmlNamespace = xmlNamespace;
ClrNamespace = clrNamespace;
}
public string XmlNamespace { get; }
public string ClrNamespace { get; }
}
}
namespace ILSpy.BamlDecompiler.Tests
{
/// <summary>
/// Tests for the fallback used when neither the BAML xmlns records nor the PI mappings name an
/// XML namespace for a type: which of the assembly's XmlnsDefinition mappings is picked.
/// </summary>
[TestFixture]
public class XmlNamespaceResolutionTests
{
public const string Winfx2006Presentation = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
public const string Netfx2007Presentation = "http://schemas.microsoft.com/netfx/2007/xaml/presentation";
public const string TestClrNamespace = "ILSpy.BamlDecompiler.Tests";
static IModule GetTestAssemblyModule()
{
var location = typeof(XmlNamespaceResolutionTests).Assembly.Location;
using var stream = new FileStream(location, FileMode.Open, FileAccess.Read);
var file = new PEFile(location, stream, streamOptions: PEStreamOptions.PrefetchEntireImage);
var resolver = new UniversalAssemblyResolver(location, throwOnError: false,
file.DetectTargetFrameworkId(), file.DetectRuntimePack());
return new BamlDecompilerTypeSystem(file, resolver).MainModule;
}
[Test]
public void PrefersPresentationNamespace_WhenDocumentDeclaresNothing()
{
var xmlNs = XamlContext.TryGetXmlNamespace(GetTestAssemblyModule(), TestClrNamespace);
Assert.That(xmlNs, Is.EqualTo(Winfx2006Presentation));
}
[Test]
public void PrefersNamespaceDeclaredByDocument_OverPresentationNamespace()
{
// Issue #1688: the document binds the default prefix to the netfx/2007 presentation
// namespace. Resolving its elements to the winfx/2006 one made the root start tag both
// declare and redefine the default prefix, which XmlWriter rejects.
var root = new XElement(XName.Get("Root", Netfx2007Presentation),
new XAttribute("xmlns", Netfx2007Presentation));
var xmlNs = XamlContext.TryGetXmlNamespace(GetTestAssemblyModule(), TestClrNamespace, root);
Assert.That(xmlNs, Is.EqualTo(Netfx2007Presentation));
}
[Test]
public void PrefersNamespaceDeclaredByAncestor()
{
var child = new XElement(XName.Get("Child", Netfx2007Presentation));
new XElement(XName.Get("Root", Netfx2007Presentation),
new XAttribute("xmlns", Netfx2007Presentation), child);
var xmlNs = XamlContext.TryGetXmlNamespace(GetTestAssemblyModule(), TestClrNamespace, child);
Assert.That(xmlNs, Is.EqualTo(Netfx2007Presentation));
}
}
}
Loading…
Cancel
Save