i have support legacy visual basic 6.0 client, needs parse xml files. these described large , complex xsd schema. in order ease parsing process, created c# classes via windows sdk xsd.exe tool, added these c# library project, , set "make assembly com-visible" attribute. unfortunately, resulting type library of no value, since merely exposes empty interfaces complex types.
to illustrate behavior, consider following xsd schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/xmlschema" targetnamespace="urn:customers" xmlns:c="urn:customers"> <xsd:element name="catalog" type="c:catalogdata"/> <xsd:complextype name="addressdata"> <xsd:sequence> <xsd:element name="no" type="xsd:integer"/> <xsd:element name="road" type="xsd:string"/> </xsd:sequence> </xsd:complextype> <xsd:complextype name="customerdata"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="address" type="c:addressdata"/> <xsd:element name="order_date" type="xsd:date"/> </xsd:sequence> <xsd:attribute name="id" type="xsd:string"/> </xsd:complextype> <xsd:complextype name="catalogdata"> <xsd:sequence> <xsd:element name="customer" type="c:customerdata" minoccurs="0" maxoccurs="unbounded"/> </xsd:sequence> </xsd:complextype> </xsd:schema> the xsd tool creates following source file:
//------------------------------------------------------------------------------ // <auto-generated> // code generated tool. // runtime version:4.0.30319.34209 // // changes file may cause incorrect behavior , lost if // code regenerated. // </auto-generated> //------------------------------------------------------------------------------ using system.xml.serialization; // // source code auto-generated xsd, version=4.0.30319.33440. // /// <remarks/> [system.codedom.compiler.generatedcodeattribute("xsd", "4.0.30319.33440")] [system.serializableattribute()] [system.diagnostics.debuggerstepthroughattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(namespace="urn:customers")] [system.xml.serialization.xmlrootattribute("catalog", namespace="urn:customers", isnullable=false)] public partial class catalogdata { private customerdata[] customerfield; /// <remarks/> [system.xml.serialization.xmlelementattribute("customer", form=system.xml.schema.xmlschemaform.unqualified)] public customerdata[] customer { { return this.customerfield; } set { this.customerfield = value; } } } /// <remarks/> [system.codedom.compiler.generatedcodeattribute("xsd", "4.0.30319.33440")] [system.serializableattribute()] [system.diagnostics.debuggerstepthroughattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(namespace="urn:customers")] public partial class customerdata { private string namefield; private addressdata addressfield; private system.datetime order_datefield; private string idfield; /// <remarks/> [system.xml.serialization.xmlelementattribute(form=system.xml.schema.xmlschemaform.unqualified)] public string name { { return this.namefield; } set { this.namefield = value; } } /// <remarks/> [system.xml.serialization.xmlelementattribute(form=system.xml.schema.xmlschemaform.unqualified)] public addressdata address { { return this.addressfield; } set { this.addressfield = value; } } /// <remarks/> [system.xml.serialization.xmlelementattribute(form=system.xml.schema.xmlschemaform.unqualified, datatype="date")] public system.datetime order_date { { return this.order_datefield; } set { this.order_datefield = value; } } /// <remarks/> [system.xml.serialization.xmlattributeattribute()] public string id { { return this.idfield; } set { this.idfield = value; } } } /// <remarks/> [system.codedom.compiler.generatedcodeattribute("xsd", "4.0.30319.33440")] [system.serializableattribute()] [system.diagnostics.debuggerstepthroughattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(namespace="urn:customers")] public partial class addressdata { private string nofield; private string roadfield; /// <remarks/> [system.xml.serialization.xmlelementattribute(form=system.xml.schema.xmlschemaform.unqualified, datatype="integer")] public string no { { return this.nofield; } set { this.nofield = value; } } /// <remarks/> [system.xml.serialization.xmlelementattribute(form=system.xml.schema.xmlschemaform.unqualified)] public string road { { return this.roadfield; } set { this.roadfield = value; } } } the generated type library looks this:
// generated .idl file (by ole/com object viewer) // // typelib filename: xsd.tlb [ ] library xsd { importlib("mscorlib.tlb"); importlib("stdole2.tlb"); // forward declare types defined in typelib interface _catalogdata; interface _customerdata; interface _addressdata; [ ] coclass catalogdata { [default] interface _catalogdata; interface _object; }; [ ] coclass customerdata { [default] interface _customerdata; interface _object; }; [ ] coclass addressdata { [default] interface _addressdata; interface _object; }; [ ] interface _catalogdata : idispatch { }; [ ] interface _customerdata : idispatch { }; [ ] interface _addressdata : idispatch { }; }; i aware, create required com interfaces manually in order expose nested properties. however, due complex xsd schema, generated c# class file on 3000 lines long , take me forever create interface every partial class.
is there alternative, speed process? or know of tool, can generate com interfaces / classes xsd schema, preferably via atl or c++?
you used project > properties > application > assembly information button , ticked "make assembly com visible" option. quick way make public classes default constructor in assembly visible com client apps. uses default value [classinterface] attribute, since isn't otherwise explicitly applied classes, classinterfacetype.autodispatch.
which safe setting, helps client code bit more resilient changes in exposed classes. runtime errors you'll when classes change client app isn't recompiled friendlier interpret. binding has nastier failure modes, including using wrong property or client app falling on over accessviolation exception.
considering exposing data, apt change frequently, that's not bad idea.
but not asking for. changing default [classinterface] simple. open properties > assemblyinfo.cs source code file , make this:
// setting comvisible false makes types in assembly not visible // com components. if need access type in assembly // com, set comvisible attribute true on type. [assembly: comvisible(true)] [assembly: classinterface(classinterfacetype.autodual)] the last line added. rebuild project , you'll see interfaces no longer empty , auto-completion works in vb6 ide.
Comments
Post a Comment