5

i have an xsd file and need to create an xml from it. Some pages assume to use the xsd.exe from Visual Studio. But how do I link the generated class to the xsd, to create xml files from it ?

Or is there another way to export values via the xsd schema to an xml file ?

1 Answer 1

19

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. The way the Serializer was to used was what i missed. Now it's working fine.
XSD Command Line Note: You may want to add the Namespace to the output file. So xsd.exe c:\stack\person.xsd /classes /language:vb /namespace:MyNamespace /out:c:\stack\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.