Wpf: XamlXmlWriter is including the namespace for each element

Created on 18 Jun 2020  路  5Comments  路  Source: dotnet/wpf

  • .NET Core Version: (e.g. 3.0 Preview1, or daily build number, use dotnet --info)

Preview6. Regression from Preview5.

  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes/No

No.

Problem description:

XamlXmlWriter is including the namespace where it wasn't before.

Minimal repro:

            XamlSchemaContext xsc = new XamlSchemaContext();
            var generated = new StringBuilder();
            var xmlSettings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true };

            var hashSetAssemblyName = typeof(HashSet<List<BigContainer>>).GetAssemblyName();
            var listAssemblyName = typeof(List<Dictionary<int, HashSet<List<BigContainer>>>>).GetAssemblyName();

            using (XamlXmlWriter writer = new XamlXmlWriter(XmlWriter.Create(new StringWriter(generated), xmlSettings), xsc))
            {
                XamlType list = xsc.GetXamlType(typeof(List<Dictionary<int, HashSet<List<BigContainer>>>>));
                var ns1 = $"clr-namespace:System.Collections.Generic;assembly={hashSetAssemblyName}";
                var ns2 = "clr-namespace:Test.Elements;assembly=XamlTestClasses";
                var ns3 = "http://schemas.microsoft.com/winfx/2006/xaml";
                writer.WriteNamespace(new NamespaceDeclaration(ns1, "a"));
                writer.WriteNamespace(new NamespaceDeclaration(ns2, "b"));
                writer.WriteNamespace(new NamespaceDeclaration(ns3, "c"));
                writer.WriteStartObject(list);
                writer.WriteEndObject();
            }


            var expected = @"<List c:TypeArguments=""Dictionary(c:Int32, a:HashSet(List(b:BigContainer)))"" xmlns=""clr-namespace:System.Collections.Generic;assembly={0}"" xmlns:a=""clr-namespace:System.Collections.Generic;assembly={1}"" xmlns:b=""clr-namespace:Test.Elements;assembly=XamlTestClasses"" xmlns:c=""http://schemas.microsoft.com/winfx/2006/xaml"" />";
            expected = string.Format(expected, listAssemblyName, hashSetAssemblyName);

            Assert.AreEqual(expected, generated.ToString());

--
聽 | 聽 | - | 聽 | "<List c:TypeArguments=\"Dictionary(c:Int32, a:HashSet(List(b:BigContainer)))\"聽xmlns=\"clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib\"聽xmlns:a=\"clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib\"聽xmlns:b=\"clr-namespace:Test.Elements;assembly=XamlTestClasses\"聽xmlns:c=\"http://schemas.microsoft.com/winfx/2006/xaml\"聽/>"\r\n
1 | 聽 | + | 聽 | {<a:List c:TypeArguments="a:Dictionary(c:Int32, a:HashSet(a:List(b:BigContainer)))"聽xmlns:a="clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib"聽xmlns:b="clr-namespace:Test.Elements;assembly=XamlTestClasses"聽xmlns:c="http://schemas.microsoft.com/winfx/2006/xaml"聽/>}
COMMENTS:
issue-type-test-bug

Most helpful comment

The output is syntactically legal and semantically correct, and it actually looks like an improvement to me. Prior to Preview6, the first namespace is written out twice: once without an name prefix, as the default namespace, and once with a name prefix.

xmlns=\"clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib\" xmlns:a=\"clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib\" 

In Preview6, there is no default namespace, and the first namespace is only written once. The absence of the default namespace requires that each element also get a name prefix.

xmlns:a="clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib" 
<a:List c:TypeArguments=...>

This is what I would expect the line below, rather than having the writer give a default namespace _and_ a name prefix for the same namespace.

writer.WriteNamespace(new NamespaceDeclaration(ns1, "a"));

So to answer your question, yes, this is syntactically legal and semantically correct and actually looks better than before, but the writer output is changed from previous releases.

And this is not a bug unless WPF provides a guarantee about the consistency of the output of the XamlXmlWriter, release to release.

All 5 comments

Is this a true bug, or a change in formatting that鈥檚 both syntactically legal and semantically correct but perhaps happens to break a test etc?

The output is syntactically legal and semantically correct, and it actually looks like an improvement to me. Prior to Preview6, the first namespace is written out twice: once without an name prefix, as the default namespace, and once with a name prefix.

xmlns=\"clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib\" xmlns:a=\"clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib\" 

In Preview6, there is no default namespace, and the first namespace is only written once. The absence of the default namespace requires that each element also get a name prefix.

xmlns:a="clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib" 
<a:List c:TypeArguments=...>

This is what I would expect the line below, rather than having the writer give a default namespace _and_ a name prefix for the same namespace.

writer.WriteNamespace(new NamespaceDeclaration(ns1, "a"));

So to answer your question, yes, this is syntactically legal and semantically correct and actually looks better than before, but the writer output is changed from previous releases.

And this is not a bug unless WPF provides a guarantee about the consistency of the output of the XamlXmlWriter, release to release.

We don鈥檛 provide preview-to-preview compat.

Serialization compatibility is something you should take into consideration between netcoreapp3.1 & net48, net5.0 & netcoreapp3.1, net5.0 & net4.8.

Serialization compat doesn鈥檛 mean that the output of XamlXmlWriter remains static; it just means that it remains compatible within reason, and in the context of safe (ie secure) [de]serialization actions.

I strongly suspect that a valid XML would remain compatible.

In that case, it sounds like the DRT should be updated. This is definitely not blocking Preview6.

Sounds like tests need to be updated here.

Was this page helpful?
0 / 5 - 0 ratings