Skip to content

Configuration options for XmlRootAttribute and XmlIncludeAttribute #567

@marcostettler

Description

@marcostettler

We've got the following situation:

Put it simple, there are three schemas, one of which is “shared” and defines a complex type that is used as the root element in the other two. Specifically, these are quite complex schemes of a SOAP interface, which are not in our responsibility.

The generated code looks like this (stripped for readability):

namespace mycomp.A
{
    ...
    [System.Xml.Serialization.XmlRootAttribute("ElementA2", Namespace="urn:mycompany:A")]
    public partial class ElementA2 : mycomp.shared.BaseType
  
namespace mycomp.B
{
    .... 
    [System.Xml.Serialization.XmlRootAttribute("ElementB1", Namespace="urn:mycompany:B")]
    public partial class ElementB1 : mycomp.shared.BaseType
    
    ....
    [System.Xml.Serialization.XmlRootAttribute("ElementB2", Namespace="urn:mycompany:B")]
    public partial class ElementB2 : mycomp.shared.BaseType   
namespace mycomp.shared
{
    ...
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(ElementA1))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(mycomp.A.ElementA2))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(mycomp.B.ElementB1))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(mycomp.B.ElementB2))]
    public partial class BaseType ...

    ...
    [System.Xml.Serialization.XmlRootAttribute("ElementA1", Namespace="urn:mycompany:A")]
    public partial class ElementA1 : BaseType
    {
    }
}

We have two problems with this code:

  1. All root elements must be generated in the corresponding namespace. “ElementA1” must be in the namespace “mycomp.A”.

  2. It must be possible to prevent the generation of XmlIncludeAttribute. The generated code is reused in different contexts, and not all namespaces are always available.

What I did:

XmlSchemaClassGenerator/ModelBuilder.cs

@@ -322,7 +322,7 @@
         else
         {
             if (type is ClassModel classModel)
-                classModel.Documentation.AddRange(GetDocumentation(rootElement));
+                derivedClassModel = CreateDerivedRootClass(rootElement, type, classModel);
 
             type.RootElement = rootElement;
             type.RootElementName = rootElement.QualifiedName;
@@ -369,8 +369,8 @@
 
         derivedClassModel.RootElementName = rootElement.QualifiedName;
 
-        if (!type.IsAbstractRoot)
-            CreateOriginalRootClass(rootElement, type, classModel);
+        // if (!type.IsAbstractRoot)
+        //     CreateOriginalRootClass(rootElement, type, classModel);
 
         return derivedClassModel;
     }

XmlSchemaClassGenerator/TypeModel.cs

@@ -417,9 +417,9 @@
             customAttributes.Add(rootAttribute);
         }
 
-        var derivedTypes = GetAllDerivedTypes();
-        foreach (var derivedType in derivedTypes.OrderBy(t => t.Name))
-            customAttributes.Add(AttributeDecl<XmlIncludeAttribute>(new CodeAttributeArgument(new CodeTypeOfExpression(derivedType.GetReferenceFor(Namespace)))));
+        //var derivedTypes = GetAllDerivedTypes();
+        //foreach (var derivedType in derivedTypes.OrderBy(t => t.Name))
+        //    customAttributes.Add(AttributeDecl<XmlIncludeAttribute>(new CodeAttributeArgument(new CodeTypeOfExpression(derivedType.GetReferenceFor(Namespace)))));
 
         classDeclaration.BaseTypes.AddRange(Interfaces.Select(i => i.GetReferenceFor(Namespace)).ToArray());

With these changes, the generated code corresponds pretty closely to what we would expect. However, the changes are untested and not cleanly implemented. The complete patch, including tests and test data, is attached.

Patch-XmlRoot-XmlInclude-Attributes.txt

We need this change, so we can contribute more. However, before I invest more time, I wanted to get your opinion on the feature request and how to proceed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions