【问题标题】:XSD Schema to COM interfacesXSD 架构到 COM 接口
【发布时间】:2015-10-10 02:46:32
【问题描述】:

我必须支持需要解析 XML 文件的旧版 Visual Basic 6.0 客户端。这些由相当大且复杂的 XSD 模式描述。为了简化解析过程,我通过 Windows SDK xsd.exe 工具创建了 C# 类,将这些类添加到 C# 库项目中,并设置“Make assembly COM-Visible”属性。不幸的是,生成的类型库没有任何价值,因为它只是为所有复杂类型公开了空接口。

为了说明这种行为,请考虑以下 XSD 架构:

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

xsd 工具创建以下源文件:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.34209
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by 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 {
        get {
            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 {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public AddressData address {
        get {
            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 {
        get {
            return this.order_dateField;
        }
        set {
            this.order_dateField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id {
        get {
            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 {
        get {
            return this.noField;
        }
        set {
            this.noField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string road {
        get {
            return this.roadField;
        }
        set {
            this.roadField = value;
        }
    }
}

生成的类型库如下所示:

// Generated .IDL file (by the OLE/COM Object Viewer)
// 
// typelib filename: xsd.tlb

[
]
library xsd
{

    importlib("mscorlib.tlb");

    importlib("stdole2.tlb");

    // Forward declare all types defined in this 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 {
    };
};

我知道,我可以手动创建所需的 COM 接口以公开所有嵌套属性。然而,由于复杂的 XSD 架构,生成的 C# 类文件超过 3000 行,我需要永远为每个部分类创建接口。

是否有替代方案可以加快流程?或者有人知道另一种工具,它可以从 XSD 模式生成 COM 接口/类,最好是通过 ATL 或 C++?

【问题讨论】:

  • 这就是 XSD 的全部内容:值类的接口定义的递归包。听起来您真正想要的是一个 XSD 编译器,它可以创建具有 XML 持久性支持的具体类实现,它可以输出 VB6 源代码。由于需求低,不要指望找到免费或便宜的东西,但有人可能已经出版了。搜索出现了这个liquid-technologies.com/xml-data-binding.aspx?view=features
  • 同样的服装也发布了codeproject.com/Articles/2299/…,虽然它现在已经很老了,可能不够用。

标签: c# c++ xsd com vb6


【解决方案1】:

您可能使用了“项目”>“属性”>“应用程序”>“程序集信息”按钮并勾选了“使程序集 COM 可见”选项。使程序集中具有默认构造函数的所有公共类对 COM 客户端应用程序可见的一种非常快速的方法。它使用[ClassInterface] attribute 的默认值,因为它没有以其他方式显式应用于类,它是ClassInterfaceType.AutoDispatch

这是一个非常安全的设置,它有助于客户端代码对公开类中的更改更具弹性。当类更改但客户端应用程序未重新编译时,您将得到的运行时错误更易于解释。早期绑定有很多更糟糕的失败模式,包括使用完全错误的属性或客户端应用程序因 AccessViolation 异常而崩溃。

考虑到您正在公开数据,容易频繁更改,这并不是一个坏主意。

但不是你要求的。更改默认的 [ClassInterface] 非常简单。打开 Properties > AssemblyInfo.cs 源代码文件,使其如下所示:

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]
[assembly: ClassInterface(ClassInterfaceType.AutoDual)]

添加了最后一行。重新构建您的项目,您现在将看到界面不再为空,并且自动完成功能在 VB6 IDE 中有效。

【讨论】:

    【解决方案2】:

    您可以“修复”xsd.exe 工具,使其生成您需要的内容。基本上,xsd.exe 是一个基于 CodeDom 的相对较小的工具。在此处查看更多信息:writing your own xsd.exe

    这是一个“自定义 xsd.exe”的起点(完整源代码)(除了类,它还生成接口和 COM 互操作的东西)

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Xml.Serialization;
    using System.Xml.Schema;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    
    using Microsoft.CSharp;
    
    namespace ConsoleApplication9
    {
        class Program
        {
            static void Main(string[] args)
            {
                // identify the path to the xsd
                const string xsdFileName = @"schema.xsd";
                var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var xsdPath = Path.Combine(path, xsdFileName);
    
                // load the xsd
                XmlSchema xsd;
                using (var stream = new FileStream(xsdPath, FileMode.Open, FileAccess.Read))
                {
                    xsd = XmlSchema.Read(stream, null);
                }
    
                var xsds = new XmlSchemas();
                xsds.Add(xsd);
                xsds.Compile(null, true);
                var schemaImporter = new XmlSchemaImporter(xsds);
    
                // create the codedom
                var codeNamespace = new CodeNamespace("Generated");
                var codeExporter = new XmlCodeExporter(codeNamespace);
    
                var maps = new List<XmlTypeMapping>();
                foreach (XmlSchemaType schemaType in xsd.SchemaTypes.Values)
                {
                    maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName));
                }
                foreach (XmlSchemaElement schemaElement in xsd.Elements.Values)
                {
                    maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName));
                }
                foreach (var map in maps)
                {
                    codeExporter.ExportTypeMapping(map);
                }
    
                PostProcess(codeNamespace);
    
                // Check for invalid characters in identifiers
                CodeGenerator.ValidateIdentifiers(codeNamespace);
    
                // output the C# code
                var codeProvider = new CSharpCodeProvider();
                using (var writer = new StringWriter())
                {
                    codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, new CodeGeneratorOptions());
                    Console.WriteLine(writer.GetStringBuilder().ToString());
                }
            }
            // For each class declaration, 
            // adds interface declaration, makes that class inherit from that interface,
            // and adds COM interop stuff
            private static void PostProcess(CodeNamespace codeNamespace)
            {
                var codeTypeDeclarations = new List<CodeTypeDeclaration>();
                foreach (CodeTypeDeclaration codeType in codeNamespace.Types)
                {
                    // mark class as com visible
                    AddClassInterfaceNone(codeType.CustomAttributes);
                    AddComVisibleTrue(codeType.CustomAttributes);
    
                    // create new interface
                    var itf = new CodeTypeDeclaration
                    {
                        Name = string.Format("I{0}", codeType.Name),
                        IsInterface = true
                    };
    
                    AddComVisibleTrue(itf.CustomAttributes);
    
                    // make base type inherit from this interface
                    codeType.BaseTypes.Add(new CodeTypeReference(itf.Name));
    
                    // clone interface members
                    foreach (CodeTypeMember m in codeType.Members)
                    {
                        var itfM = CloneMember(m);
                        itfM.CustomAttributes.Clear();
                        itf.Members.Add(itfM);
                    }
    
                    codeTypeDeclarations.Add(itf);
                }
    
                codeNamespace.Types.AddRange(codeTypeDeclarations.ToArray());
            }
    
            private static CodeTypeMember CloneMember(CodeTypeMember m)
            {
                var ms = new MemoryStream();
                var formatter = new BinaryFormatter();
                formatter.Serialize(ms, m);
                ms.Seek(0, SeekOrigin.Begin);
                return formatter.Deserialize(ms) as CodeTypeMember;
            }
    
            private static void AddComVisibleTrue(CodeAttributeDeclarationCollection attrs)
            {
                attrs.Add(new CodeAttributeDeclaration(
                    new CodeTypeReference("System.Runtime.InteropServices.ComVisibleAttribute"),
                    new[] { new CodeAttributeArgument(new CodePrimitiveExpression(true)) }));
            }
    
            private static void AddClassInterfaceNone(CodeAttributeDeclarationCollection attrs)
            {
                attrs.Add(new CodeAttributeDeclaration(
                    new CodeTypeReference("System.Runtime.InteropServices.ClassInterface"),
                    new[] { new CodeAttributeArgument(new CodeFieldReferenceExpression(
                    new CodeTypeReferenceExpression("System.Runtime.InteropServices.ClassInterfaceType"), 
                    ClassInterfaceType.None.ToString()))
                        }));
            }
        }
    }
    

    这是你得到的输出:

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("ConsoleApplication9", "1.0.0.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:customers")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="urn:customers", IsNullable=true)]
    [System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class AddressData : IAddressData {
    
        private string noField;
    
        private string roadField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")]
        public string no {
            get {
                return this.noField;
            }
            set {
                this.noField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string road {
            get {
                return this.roadField;
            }
            set {
                this.roadField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("ConsoleApplication9", "1.0.0.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:customers")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="urn:customers", IsNullable=true)]
    [System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class CustomerData : ICustomerData {
    
        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 {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public AddressData address {
            get {
                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 {
            get {
                return this.order_dateField;
            }
            set {
                this.order_dateField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id {
            get {
                return this.idField;
            }
            set {
                this.idField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("ConsoleApplication9", "1.0.0.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:customers")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="urn:customers", IsNullable=true)]
    [System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class CatalogData : ICatalogData {
    
        private CustomerData[] customerField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("customer", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public CustomerData[] customer {
            get {
                return this.customerField;
            }
            set {
                this.customerField = value;
            }
        }
    }
    
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public interface IAddressData {
    
    
    
        /// <remarks/>
        string no {
            get;
            set;
        }
    
        /// <remarks/>
        string road {
            get;
            set;
        }
    }
    
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public interface ICustomerData {
    
    
    
    
    
        /// <remarks/>
        string name {
            get;
            set;
        }
    
        /// <remarks/>
        AddressData address {
            get;
            set;
        }
    
        /// <remarks/>
        System.DateTime order_date {
            get;
            set;
        }
    
        /// <remarks/>
        string id {
            get;
            set;
        }
    }
    
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public interface ICatalogData {
    
    
        /// <remarks/>
        CustomerData[] customer {
            get;
            set;
        }
    }
    

    这是 TLB 文件:

    // TLib :     // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
    importlib("mscorlib.tlb");
    // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("stdole2.tlb");
    
    // Forward declare all types defined in this typelib
    interface IAddressData;
    interface ICustomerData;
    interface ICatalogData;
    
    [
      odl,
      uuid(9C2EF5B0-59BA-3CBE-874A-DA690A595F26),
      version(1.0),
      dual,
      oleautomation,
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Generated.IAddressData")    
    
    ]
    interface IAddressData : IDispatch {
        [id(0x60020000), propget]
        HRESULT no([out, retval] BSTR* pRetVal);
        [id(0x60020000), propput]
        HRESULT no([in] BSTR pRetVal);
        [id(0x60020002), propget]
        HRESULT road([out, retval] BSTR* pRetVal);
        [id(0x60020002), propput]
        HRESULT road([in] BSTR pRetVal);
    };
    
    [
      uuid(26736B67-A277-3E81-AAF1-653A936F209E),
      version(1.0),
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Generated.AddressData")
    ]
    coclass AddressData {
        interface _Object;
        [default] interface IAddressData;
    };
    
    [
      odl,
      uuid(8CCFC141-05C0-3A11-BD3B-C3279AB9B3C1),
      version(1.0),
      dual,
      oleautomation,
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Generated.ICustomerData")    
    
    ]
    interface ICustomerData : IDispatch {
        [id(0x60020000), propget]
        HRESULT name([out, retval] BSTR* pRetVal);
        [id(0x60020000), propput]
        HRESULT name([in] BSTR pRetVal);
        [id(0x60020002), propget]
        HRESULT address([out, retval] IAddressData** pRetVal);
        [id(0x60020002), propputref]
        HRESULT address([in] IAddressData* pRetVal);
        [id(0x60020004), propget]
        HRESULT order_date([out, retval] DATE* pRetVal);
        [id(0x60020004), propput]
        HRESULT order_date([in] DATE pRetVal);
        [id(0x60020006), propget]
        HRESULT id([out, retval] BSTR* pRetVal);
        [id(0x60020006), propput]
        HRESULT id([in] BSTR pRetVal);
    };
    
    [
      uuid(9B02E545-4EF6-355E-8CD3-7EC5D2780648),
      version(1.0),
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Generated.CustomerData")
    ]
    coclass CustomerData {
        interface _Object;
        [default] interface ICustomerData;
    };
    
    [
      odl,
      uuid(8DC4A69C-31FA-311A-8668-9D646AFF1F10),
      version(1.0),
      dual,
      oleautomation,
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Generated.ICatalogData")    
    
    ]
    interface ICatalogData : IDispatch {
        [id(0x60020000), propget]
        HRESULT customer([out, retval] SAFEARRAY(ICustomerData*)* pRetVal);
        [id(0x60020000), propput]
        HRESULT customer([in] SAFEARRAY(ICustomerData*) pRetVal);
    };
    
    [
      uuid(5423877C-2618-3D59-8F3A-E1443AC362CA),
      version(1.0),
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Generated.CatalogData")
    ]
    coclass CatalogData {
        interface _Object;
        [default] interface ICatalogData;
    };
    

    【讨论】:

      猜你喜欢
      • 2011-11-28
      • 1970-01-01
      • 2011-06-18
      • 2012-05-29
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多