how to create c# class from Xsd
On Windows, there's a Microsoft program called xsd.exe that will turn your XML into a schema (XSD), and generate C# classes from that. It's installed with Visual Studio on Windows. It's in C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools or similar, although if you start a Visual Studio Developer Command Prompt it will be on the path. To use it with your file:
- Save the XML data as data.xml or similar (it's XML not an XSD clearly)
- The tool doesn't like the fact that 'interface' has both an attribute called 'id' and an element called 'id', so rename one of them or take one out.
To generate an XSD file called data.xsd: start a Developer Command prompt then navigate to the folder with data.xml in and do:
xsd data.xml
To generate C# classes in a file called data.cs do:
xsd /c data.xsd
The resulting classes are serializable, so you can deserialize the XML into them.
The drawback of doing it this way is the classes you get are complex (I'm getting 340 lines of code with your XML!), so crafting your classes by hand may be better.
I wrote about this (a long long time ago) with respect to the very complex FpML schemas.
How to generate .NET 4.0 classes from xsd?
simple enough; just run (at the vs command prompt)
xsd your.xsd /classes
(which will create your.cs
). Note, however, that most of the intrinsic options here haven't changed much since 2.0
For the options, use xsd /?
or see MSDN; for example /enableDataBinding
can be useful.
XML Schema to C++ Classes
Sounds to me like CodeSynthesis is exactly what you are looking for. It's open source and c++.
How can I convert an XSD file to C# Class
Use the XML Schema Definition Tool xsd.exe
found in your framework tools to convert your schema into a serializable class or dataset.
xsd file.xsd {/classes | /dataset} [/element:element]
[/language:language] [/namespace:namespace]
[/outputdir:directory] [URI:uri]
And in example, whereas the C# class will be generated in the same directory as the xsd tool:
xsd /c YourFile.xsd
Transform XSD 1.1 schema to c# class
There is no easy way around this.
Customizing xsd.exe
behavior is generally possible, even easy. It is just a thin layer atop .NET libraries (System.Xml). But those libraries do not understand XSD 1.1, so you would have to have an XSD 1.0 document to feed them. And if you can convert your schemas into the older specification language, then you can use the unmodified xsd.exe
as well.
Could you transform an XSD 1.1 schema into an XSD 1.0 schema, for your purpose? In some instances, it might be easy. For example, a very simple XSLT transform can filter out asserts, replace the new datatypes by suitable XSD 1.0 datatypes and the like. However, the general case is somewhere between hard and unsolvable. Let's see where XSD 1.1 adds features that you can't just ignore:
<complexType name="base">
<complexContent>
<sequence>
<element ref="tns:a" minOccurs="0" maxOccurs="1"/>
<choice minOccurs="0" maxOccurs="unbounded">
<element ref="tns:b"/>
<element ref="tns:c"/>
</choice>
</sequence>
</complexContent>
</complexType>
<complexType name="derived">
<complexContent>
<restriction base="tns:base">
<sequence>
<choice minOccurs="0" maxOccurs="unbounded">
<element ref="tns:b"/>
<element ref="tns:c"/>
</choice>
</sequence>
</restriction>
</complexContent>
</complexType>
(Sample taken from here. That's a great starting point for estimating whether your particular schemas could be expressed in, or converted to, XSD 1.0.)
The sample shows a pretty natural use of inheritance in XSD 1.1. All that the derived type really does is banning the member tns:a
. To do the same thing in XSD 1.0, the type would need to list tns:a
with maxOccurs='0'
, and the corresponding C# class will contain the a
member, whether you want it or not. Such a downgrade transformation (from XSD 1.1 to XSD 1.0) is still relatively easy to put together to serve this simple example but basically impossible when you need to map particles across a whole inheritance hierarchy with such restricted optional members and/or wildcards. XSD 1.0 just isn't expressive enough.
Now to the general question. Suppose that your schemas are unknown in advance and/or making heavy use of new XSD 1.1 features. You won't be able to convert them to XSD 1.0 and so the .NET base class library won't help you generate C# classes from them. You have two or three options left:
- You can create the deserialization classes manually. You won't be able to validate and deserialize, but you will be able to deserialize a valid document, including enforcing proper attribute defaults (a typical side effect of validation that you will want to keep).
- You can create your own XSD 1.1 parsing support. A very big project, although you can ignore a lot of the schema (such as the whole XSLT 2.0 spec for asserts) and still be able to end up with somewhat natural C# classes.
- You can take an existing third party library with this feature. An empty set to choose from at the moment, I suspect. Saxon's API does not generate deserialization classes from the schema. And I'm not aware of any other XSD 1.1 implementations available on the .NET platform.
Related Topics
Load an Pem Encoded X.509 Certificate into Windows Cryptoapi
Platform-Independent Guid Generation in C++
Why Isn't 'Std::Initializer_List' Defined as a Literal Type
Localtime VS Localtime_S and Appropriate Input Arguments
How to Use an Enum Value in a Switch Statement in C++
Unit Testing of Private Methods
C++, How to Call a Constructor Directly, Without New
Is Assignment Operator '=' Atomic
How to Install/Configure Opencv3.2.0 with C++, Visual Studio 2017
Detect Operator Support with Decltype/Sfinae
Multiple Inheritance + Virtual Function Mess
Performance Penalty for Working with Interfaces in C++
Using Sfinae for Template Class Specialisation
What Does _T Stands for in a Cstring
How to Have Polymorphic Containers with Value Semantics in C++