Link to home
Start Free TrialLog in
Avatar of Wayne29
Wayne29

asked on

How to create XSD file dynamically based on XML file?

How can I create XSD file based on the XML file programatically using asp.net (C# / VB.Net)?

I know the command line utilities xsdinference tool to do the same.

But i WOULD like to do the same dynamically through my code.

<sample>
<title>Mr.</title>
<forename>Wayne</forename>
<surname>Feraera</feraera>
<Address1>Canb</Address1>
<Address2>Australia</Address2>
</sample>
Avatar of Wayne29
Wayne29

ASKER

It's Ok Guys. I got it.
Please find the answer as below.
Thanks.

' Create XmlReader to pass as argument.
        Dim xtr As New XmlTextReader(Server.MapPath(".") + "\XMLFile1.xml")

        ' Create the XmlSchemaCollection that the inferred schema is
        ' added to.
        Dim xsc As New XmlSchemaCollection


        ' Create Infer object.
        Dim objInfer As New Infer

        ' Infer schema
        xsc = objInfer.InferSchema(xtr)
        Dim st As New System.Text.StringBuilder

        Dim xs As XmlSchema
        For Each xs In xsc
            Dim writer = New StringWriter
            xs.Write(writer)
            st.Append(writer.ToString())

            Dim xsi As XmlSchemaImport
            For Each xsi In xs.Includes
                Dim strWriter = New StringWriter
                xs.Write(strWriter)
                st.Append(strWriter.ToString())
            Next xsi
        Next xs


        Dim x As String = st.ToString()
        Dim y As New StreamWriter(Server.MapPath(".") + "\NewSchema2.xsd")
        y.Write(x)
        y.Close()
ASKER CERTIFIED SOLUTION
Avatar of EE_AutoDeleter
EE_AutoDeleter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial