将类文件拆分为多个文件而不是一个文件

时间:2015-06-26 20:55:11

标签: xml vb.net xsd codedom xmlschemaset

我有以下代码,它接受一个xml文件,并根据xml文件的内容将其转换为一组公共部分类:

Public Class XMLToClass
    Implements IXMLToClass

    Public Sub New()
    End Sub

    Public Function GetData(ByVal i As Integer) As String Implements IXMLToClass.GetData
        Return String.Empty
    End Function

    Public Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType Implements IXMLToClass.GetDataUsingDataContract
        Return composite
    End Function

    Public Sub GenerateClassFromXML(ByVal XMLfileName As String, ByVal OutPutFilePath As String, ByVal CodeNameSpace As String)

        Dim xs As New XmlSchemaInference()
        Dim schemaSet As XmlSchemaSet
        Dim schemas As New XmlSchemas()
        Using xr As XmlReader = XmlReader.Create(XMLfileName)
            xs.TypeInference = XmlSchemaInference.InferenceOption.Relaxed
            xs.Occurrence = XmlSchemaInference.InferenceOption.Relaxed
            schemaSet = xs.InferSchema(xr)
        End Using

        For Each Schema As XmlSchema In schemaSet.Schemas
            schemas.Add(Schema)
        Next
        PersistClass(schemas, OutPutFilePath, CodeNameSpace)

    End Sub

    Public Sub PersistClass(ByVal schemas As XmlSchemas, ByVal OutPutFileName As String, ByVal namesp As String)

        Dim ns As CodeNamespace = XsdGenerator.Processor.Process(schemas, namesp)
        Dim provider As CodeDomProvider
        provider = New Microsoft.VisualBasic.VBCodeProvider()
        Using sw As New StreamWriter(OutPutFileName, False)
            provider.GenerateCodeFromNamespace(ns, sw, New CodeGeneratorOptions())
        End Using

    End Sub

End Class

Namespace XsdGenerator

    Public NotInheritable Class Processor
        Public Shared Function Process(ByVal schemas As XmlSchemas, ByVal targetNamespace As String) As CodeNamespace

            Dim importer As New XmlSchemaImporter(schemas)
            Dim ns As New CodeNamespace(targetNamespace)
            Dim exporter As New XmlCodeExporter(ns)
            For Each xsd As XmlSchema In schemas
                For Each element As XmlSchemaElement In xsd.Elements.Values
                    Dim mapping As XmlTypeMapping = importer.ImportTypeMapping(element.QualifiedName)
                    exporter.ExportTypeMapping(mapping)
                Next
            Next
            Return ns
        End Function
    End Class

End Namespace

有没有办法可以将每个类保存为单独的文件,而不是将它们全部保存在一个文件中?

0 个答案:

没有答案