如何使用带有attributeGroup ref的XSD.exe

时间:2017-05-17 14:42:24

标签: c# xml xsd xsd.exe

在使用xsd.exe ref时使用attributeGroup时遇到问题。我用它来生成C#类。

这是我的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>

它生成这两个类:

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;
        }
    }
}

生成的类缺少来自Name的字段BornPersonBaseAttributes我的XSD不正确或者xsd.exe不知道如何处理它?<​​/ strong>

如果 xsd.exe 无法处理,还有其他办法吗?

我这样执行:

xsd.exe foo.xsd /c

2 个答案:

答案 0 :(得分:1)

XML Schema对我来说是正确的,没有属性BossSales的元素NameBorn对模式无效(例如,oXygen需要随架构提供时的这些属性。)

请注意,生成的代码由部分类组成。该工具是否可以在其他地方生成其他属性?

答案 1 :(得分:1)