Problem with Code Generated by XSD.EXE: Sequence of Elements is Generated as an Array

£可爱£侵袭症+ 提交于 2019-12-25 15:15:14

问题


I am trying to generate C# code from an XSD using xsd.exe

Here is a snippet of the problematic area

<xs:element name="EmailConfiguration" minOccurs="1" maxOccurs="1">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="DefaultSendToAddressCollection" minOccurs="0" maxOccurs="1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="EmailAddress" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>

        </xs:sequence>
      </xs:complexType>
    </xs:element>

Currently DefaultSendToAddressCollection is being generated as a string[]

How can I change the xsd, so that it is generated as a strong type, and email addresses as a collection to the strong type?

Question Update:

Or is xsd.exe bugged?


回答1:


You've specified EmailAddress to be of type xs:string instead of a complex type - therefore, DefaultSendToAddressCollection is an array of strings, instead of a collection of objects.

If you change EmailAddress to be a complex type, and give it an xs:attribute of type xs:string to store the address to, you will end up with a collection of EmailAddress objects.

<xs:element name="EmailConfiguration" minOccurs="1" maxOccurs="1">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="DefaultSendToAddressCollection" minOccurs="0" maxOccurs="1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="EmailAddress" minOccurs="1" maxOccurs="unbounded">
                  <xs:complexType>
                    <xs:attribute name="Address" type="xs:string" />
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>


来源:https://stackoverflow.com/questions/1165999/problem-with-code-generated-by-xsd-exe-sequence-of-elements-is-generated-as-an

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!