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.
How to generate .NET 4.0 classes from xsd with more than 1 element to class?
The reason you only get 1 element is because to be valid the xml must always have a single outer root node. What is suprising to me is that the second node was just ignored completely and no exception was thrown.
Your xsd represents the following xml
<advice_file/>
To have both elements, you need to write you xsd as follows:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="myRoot">
<xs:complexType>
<xs:sequence>
<xs:element name="advice_file" type="xs:string"/>
<xs:element name="vendorMerchID" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
for the xml structure as:
<myRoot>
<advice_file/>
<vendorMerchID/>
</myRoot>
Alternatively, if your xml is like this:
<advice_file>
<vendorMerchID/>
</advice_file>
Use the xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="advice_file">
<xs:complexType>
<xs:sequence>
<xs:element name="vendorMerchID" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Generating Classes from XSD schema
you can use xsd2code VS extension, its much more powerful than Microsoft xsd tool, there are options to support WinRT.
however its not free, but you can use trial version which is fully functional for 15 days.
Note that current version is not compatible with VS 2017, but it works for older visual studios.
vb.net - Create Class from XSD and generate xml
If you want to create a XML document that is based on a XSD there are a few steps you need to walk through.
1) You will need to create .NET classes based on your XSD.
2) You will need to create a new instance of that class and serialize the output.
Step 1 - Create a .NET Class from a XSD Document
A XSD file provides the blue print for a class. Here is a example of a XSD file:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Person" nillable="true" type="Person" />
<xs:complexType name="Person">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="firstName" type="xs:string" />
<xs:element minOccurs="0" maxOccurs="1" name="lastName" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="dateOfBirth" type="xs:dateTime" />
<xs:element minOccurs="1" maxOccurs="1" name="gender" type="Gender" />
<xs:element minOccurs="1" maxOccurs="1" name="height" type="xs:int" />
<xs:element minOccurs="1" maxOccurs="1" name="weight" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="Gender">
<xs:restriction base="xs:string">
<xs:enumeration value="Male" />
<xs:enumeration value="Female" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
Create a new folder to work in. I'm using 'C:\STACK'.
Create new text file, copy and paste the XSD into it and save it as 'person.xsd'.
Now we need to use the XSD.exe to convert this file into a class.
You will need to find the XSD exe on your machine, for me it was in:
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\xsd.exe
Now open command prompt and enter this
cd "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools"
Now we will create the .NET classes (here is is command broken down)
xsd.exe -Execute xsd
/classes -Create Clasess
/language:vb -Language to use (VB, CS, JS)
/out:"c:\stack\" -Output folder
c:\stack\person.xsd -The XSD File to use
Here is the command in one line
xsd.exe c:\stack\person.xsd /classes /language:vb /out:c:\stack\
After you have run this command a new file will be created 'c:\stack\person.vb'
You can then add this class into you project.
Step 2 - Create a new instance of that class and serialize the output
Now that you have added the new class, you can create a instance of it:
Dim person As New Person
person.firstName = "Mike"
person.lastName = "Bateman"
person.gender = Gender.Male
person.height = 160
person.weight = 80.3
Now we can serialize the class to a XML file:
Dim serializer As New XmlSerializer(GetType(Person))
Dim writer As New StreamWriter("c:\stack\person.xml")
serializer.Serialize(writer, person)
writer.Close()
And we can read the XML back to a .NET class like this:
Dim serializer As New XmlSerializer(GetType(Person))
Dim reader As New IO.StreamReader("c:\stack\person.xml")
Dim personRes As Person = serializer.Deserialize(reader)
reader.Close()
reader.Dispose()
Hope that helps!
Related Topics
String = String + Int: What's Behind the Scenes
Format an Excel Column (Or Cell) as Text in C#
Nullable Type Is Not a Nullable Type
Hyphenated HTML Attributes with ASP.NET MVC
Converting Bitmap Pixelformats in C#
Convert a Binary String Representation to a Byte Array
Finding an Enum Value by Its Description Attribute
Entity Framework 4 Delete Object from Entity Collection
Nhibernate Aliastobean Transformer Associations
How to Set Properties on Struct Instances Using Reflection
Why Is Cross Thread Operation Exception Not Thrown While Running Exe in Bin\Debug
Deserialize a JSON Array in C#
How to Decode a Unicode Character in a String
How to Convert List of Arrays into a Multidimensional Array
Xml-Selectnodes with Default-Namespace via Xmlnamespacemanager Not Working as Expected