3

I'm having trouble using xsd.exe while using a attributeGroup with ref. I use it to generate C# classes.

Here's my XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           version="1.0">

  <xs:attributeGroup name="PersonBaseAttributes">
    <xs:attribute name="Name" type="xs:string" use="required" /> <!-- Missing in .CS -->
    <xs:attribute name="Born" type="xs:dateTime" use="required" /> <!-- Missing in .CS -->
  </xs:attributeGroup>

  <xs:attributeGroup name="SalesAttributes">
    <xs:attributeGroup ref="PersonBaseAttributes" />
    <xs:attribute name="Sales" type="xs:int" use="required" />
  </xs:attributeGroup>

  <xs:attributeGroup name="BossAttributes">
    <xs:attributeGroup ref="PersonBaseAttributes" />
    <xs:attribute name="Department" type="xs:string" use="required" />
  </xs:attributeGroup>

  <xs:element name="Boss" nillable="true" type="BossPerson" />
  <xs:element name="Sales" nillable="true" type="SalesPerson" />
  <xs:complexType name="SalesPerson">
    <xs:attributeGroup ref="SalesAttributes" />
  </xs:complexType>
    <xs:complexType name="BossPerson">
    <xs:attributeGroup ref="BossAttributes" />
  </xs:complexType>
</xs:schema>

It generates these two classes:

public partial class SalesPerson {

    private int salesField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int Sales {
        get {
            return this.salesField;
        }
        set {
            this.salesField = value;
        }
    }
}

public partial class BossPerson {

    private string departmentField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Department {
        get {
            return this.departmentField;
        }
        set {
            this.departmentField = value;
        }
    }
}

The generated classes are missing the fields Name and Born from PersonBaseAttributes. Is my XSD incorrect or doesn't xsd.exe know how to handle it?

And if xsd.exe cannot handle it, is there any other way to do it?

I execute it like this:

xsd.exe foo.xsd /c
smoksnes
  • 10,509
  • 4
  • 49
  • 74

2 Answers2

3

I have the same problem. I think xsd.exe does not support nested attributeGroups: https://social.msdn.microsoft.com/Forums/en-US/707c8a47-a29f-4262-b052-ac66dc99d604/nested-xml-attribute-groups?forum=asmxandxml

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
gfarkasd
  • 31
  • 2
  • This is an answer. The answer is saying "You can't do that, it's not supported". If you feel that that is incorrect or not useful, the correct action to take is to downvote the answer, not to flag it. – Robert Columbia Apr 15 '18 at 10:46
1

The XML Schema looks correct to me, an element Boss or Sales without the attributes Name and Born would be invalid against the schema (e.g., oXygen does require these attributes when supplied with your schema).

Note that the generated code is made of partial classes. Could the tool have generated the other attributes somewhere else?

Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37