Help with created .xsd when creating xml file


I'm using the code below to create an xml file from a dataset. How do I also create the schema (.xsd)?

dtsetCtry.WriteXml(Application.StartupPath + "\Data\Ctry.xml")


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GanapathiFacets DeveloperCommented:
You can use the inbuilt xsd.exe tool which will generate the XSD based on the DataSet dynamially.

Refer the well illustrated link below.
vcharlesAuthor Commented:
Thank you for the link will get back to you.
vcharlesAuthor Commented:

Do you how to achieve the same by code?


Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Fernando SotoRetiredCommented:
Hi Victor;

You can use the Process Class to run the xsd.exe file supplying the XML file as input and it will produce the xsd file. Note that this program is part of the Visual Studio Tools and if you are building an application to be distributed then you will need to also package it with the application.
vcharlesAuthor Commented:
Thank You. I will try it and get back to you.
vcharlesAuthor Commented:
I tried Process.Start("Xsd.exe", "C:\Test.xml")

But received error message:

The system can not find the file specified, I'm certain the file is in the C folder.

Fernando SotoRetiredCommented:
You will need to use the full path to the file xsd.exe. It is located in the Visual Studio Install directory in a tools folder. Also unless test.xml is located in the C drive root directory you will also nee a full path for that as well.
vcharlesAuthor Commented:

Won't hard coding the full path for the xsd
cause an error when I deploy the application?
vcharlesAuthor Commented:

Please disregard last message
vcharlesAuthor Commented:

I can't seem to find xsd.exe, will keep looking.

vcharlesAuthor Commented:

Below is the complete code from the link, I'm only using the process statement could that also be the reason why it can not find the path?

Imports System
Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
            ' Opens the Internet Explorer application.
            Public Sub OpenApplication(myFavoritesPath As String)
            ' Start Internet Explorer. Defaults to the home page.

            ' Display the contents of the favorites folder in the browser.
        End Sub 'OpenApplication

        ' Opens urls and .html documents using Internet Explorer.
        Sub OpenWithArguments()
            ' url's are not considered documents. They can only be opened
            ' by passing them as arguments.
            Process.Start("IExplore.exe", "")

            ' Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
            Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
        End Sub 'OpenWithArguments

        ' Uses the ProcessStartInfo class to start new processes,
        ' both in a minimized mode.
        Sub OpenWithStartInfo()
            Dim startInfo As New ProcessStartInfo("IExplore.exe")
            startInfo.WindowStyle = ProcessWindowStyle.Minimized


            startInfo.Arguments = ""

        End Sub 'OpenWithStartInfo

        Shared Sub Main()
            ' Get the path that stores favorite links.
            Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)

            Dim myProcess As New MyProcess()

        End Sub 'Main
    End Class 'MyProcess
End Namespace 'MyProcessSample
Fernando SotoRetiredCommented:
Hi Victor;

I went through some documentation I had and found a much nicer way to create the XSD file in your program, see code snippet below.

Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

'' Create an XmlReader to read in the XML document to create an XSD file for.
'' Change the path to fit your needs. 
Dim reader As XmlReader = XmlReader.Create("C:\Working Directory\linkfile2.xml")
'' Holds the XSD elements as they are created
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
'' Used to infer the XSD from the XML file
Dim schema As XmlSchemaInference = New XmlSchemaInference()
'' Create the XSD document
schemaSet = schema.InferSchema(reader)

'' Write the XSD document to a file. Change the path to where to
'' save it on the file system
Using sw As New StreamWriter("C:\Working Directory\LinkFile2.xsd")
    For Each s As XmlSchema In schemaSet.Schemas()
End Using

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank you. I will try it and get back to you.
vcharlesAuthor Commented:

I am getting the following error:

The process cannot access the file 'C:\TestProject\bin\Debug\Data\LinkFiles\LinkTest.xml' because it is being used by another process.

On Line:

Using sw As New StreamWriter(Application.StartupPath + "\Data\LinkFiles\LinkTest.xml")

Fernando SotoRetiredCommented:
Check to make sure that the file is not already open in your program before this call.
vcharlesAuthor Commented:

It works when I use ("C:\Working Directory\LinkTest.xml")

Thank You.

vcharlesAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.