Generate Nested Types instead of Global Types with xsd.exe

核能气质少年 提交于 2019-12-07 16:45:40

问题


Using xsd.exe in a C# class, is there a way to produce a xsd file with Nested Type, instead of Global Types?

I want to use this xsd file, with SSIS - Sql Server Integration Services, and look SSIS is not reading my xsd very well.

I want to generate the xsd like this, with Nested Types :

  <?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Country">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="City">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="CityName" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="CoutryName" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

but xsd.exe produce this , with Global Types, and SSIS don't read it. I need to change this xsd manually to be like above.

 <?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Country">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="City" type="City">
        </xs:element>
        <xs:element name="CoutryName" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="City">
    <xs:sequence>
      <xs:element name="CityName" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Any suggestion? Or other tool that I can use.

Thanks a lot.


回答1:


I'll further assume that not "very well" means you're not seeing CountryName in your XML Source output. The documentation on MSDN is a good reading, although in my opinion it fails to describe why you would encounter the behavior you see.

I believe that it has to do with the way XML Source outputs are being determined. SSIS infers a data set from the XML structure; the top level entity, that corresponds to the root element, is not mapped to an output, so all the attributes associated with it, in your case CountryName, will not show up.

The easiest way to prove it, is to add another root element that wraps your Country (equivalent to having a dummy "root" class that has a Country-type property).

<xs:element name="root">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="Country"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

If you add the above schema fragment to your schema, you should get your expected results. At the beginning I thought that it has to do with the issue described here; while you can still use the tool to visualize the data set as described by the MSDN link above, in your case, the authoring style you suggested (basically a russian-doll) can't change the outcome.



来源:https://stackoverflow.com/questions/7915852/generate-nested-types-instead-of-global-types-with-xsd-exe

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