Split Class file into multiple files instead of one single file

210 views Asked by At

I have the below code that takes an xml file and converts it into a set of public partial classes based on the content of the xml file:

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

Is there a way I can have each class saved as a separate file, instead of all of them in one file?

0

There are 0 answers