How to create XSD file programmatically in C#?

22,179

Solution 1

If you just want to produce an XSD file from an XML file, then you can use Microsoft's XSD.EXE, which is included with Visual Studio.

To use it, open a Visual Studio Command Prompt. Then enter xsd file.xml to produce a schema for that XML file.

You can also use that tool to generate classes for your XML input, just pass in the /classes parameter.

Solution 2

While an XML Schema file is an XML file, it has certain things that could make it cumbersome to do it "by hand"; one could say why write XML using the DOM API instead of using C# classes generated by XSD.exe or XSD2Code.exe? Or to push it a bit... somewhat similar to someone saying C# statements eventually turn into IL assembly; why not write IL instead?

Another alternative is provided by the Schema Object Model API; in .NET, it is the System.Xml.Schema namespace.

Take a look at the code example found here on MSDN. It'll give you an idea for another approach. It provides a programmer friendly API to generate XSDs, instead of dealing with the actual XML.

Solution 3

XSD is just another XML-type file. whatever you are using to create your XML file will also be useful to create the XSD file; you just need to know what tags/attributes you want to put in the file...

Solution 4

Solution: I originally posted a reply, but Stack Overflow rather I edit my response, so here is the edit. The original tool that I suggested goes the other way. I then did some research and someone recommended a tool called MyGenerations or something like that. That required an installation, downloaded template, and oodles of work, so way too complicated. I then did some more research and came across a rather nifty solution on CodePage, but that solution, which works, because I tested it, requires some modifications. The code is called XmlToXsd with the URL:

http://www.codeproject.com/Articles/133570/XmlToXsd-A-Better-Schema-Generator.

The Programs section has two bugs. First it saves the XSD to the executable's root. Do not worry, the exe merely calls a method located in one CS file. Simply include that CS file in your project and call with the line given, just you have to change the path to the same folder as the XML. Additionally, the author uses a targetnamespace of a junk URL. DevExpress throws a complaint on the nonexistent URL. Through some effort, I replaced the target URL with null. In the accompanying CS file I had to make a few changes. Others with more understanding than I can modify to his/her hearts extent. I merely help others by telling them what I did, so no static back.

Change 1: Before "target = XNamespace.Get(targetNamespace);" add an if-statement only loading if not null for the target namespace.

Change 2: I added another if-statement for the return. The null return, a copy of the original return modifies as follows:

2.1: Nuke the targetnamespace attribute item. 2.2: After the replace call, add another replace call of ".Replace("xmlns=\"null\"", null))". 2.3: Before the return, requires adding braces, set target = "null".

The resulting file now appears totally valid and created programatically.

Yes, one can use XSD.exe but manually, but I needed a programatic way of the conversation.

Original Post: I had the same question, not necessarily programatically. The XSD utility does do the job. The problem is that Microsoft does not distribute xsd.exe with Visual Studio 2012 Professional. I searched my hard drive and found the utility with Microsoft's SDK, path on my disk "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64\xsd.exe".

I had to go to the folder where the XML file is and execute XSD utility from a command box (not in Visual Studio) and wound up with the XSD file.

One way to do this task programatically is to call XSD from code, but that would mean distributing XSD.

I found this one assembly, but never used it that might do the job. Check out: http://xsd2code.codeplex.com/

Share:
22,179
Shadi
Author by

Shadi

Updated on October 11, 2020

Comments

  • Shadi
    Shadi over 3 years

    I have a xml file (that i created in c# using XmlDocument) and now i have to create xsd file the corresponds to it also programmatically in C# ... how can I do this ?? can I use the same classes for creating xml files ? or are there any other (xsd related) classes I should use ??