Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create XSD file programmatically in C#?

Tags:

c#

xml

xsd

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 ??

like image 523
Shadi Avatar asked May 11 '12 19:05

Shadi


2 Answers

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.

like image 118
schellack Avatar answered Sep 22 '22 11:09

schellack


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.

like image 22
Petru Gardea Avatar answered Sep 22 '22 11:09

Petru Gardea