{"id":1697,"date":"2014-04-18T07:28:24","date_gmt":"2014-04-18T07:28:24","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1697"},"modified":"2014-04-18T07:28:24","modified_gmt":"2014-04-18T07:28:24","slug":"generating-classes-from-xml-using-xsd-exe","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/generating-classes-from-xml-using-xsd-exe\/","title":{"rendered":"Generating classes from XML using xsd.exe"},"content":{"rendered":"<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/x6c1kb0s(v=vs.110).aspx\" title=\"XML Schema Definition Tool\" target=\"_blank\">XML Schema Definition Tool<\/a> (xsd.exe) can be used to generate xml schema files from XML and better still C# classes from xml schema files.<\/p>\n<p><strong>Creating classes based upon an XML schema file<\/strong><\/p>\n<p>So in it&#8217;s simplest usage we can simply type<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nxsd person.xsd \/classes\r\n<\/pre>\n<p>and this generates C# classes representing the xml schema. The default output is C# but using the <em>\/language<\/em> or the shorter form <em>\/l<\/em> switch we can generate Visual Basic using the VB value, JScript using JS or CS if we wanted to explicitly static the language was to be C#. So for example using the previous command line but now to generate VB code we can write<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nxsd person.xsd \/classes \/l:VB\r\n<\/pre>\n<p>Assuming we have an xml schema, person.xsd, which looks like this<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;xs:schema elementFormDefault=&quot;qualified&quot; xmlns:xs=&quot;http:\/\/www.w3.org\/2001\/XMLSchema&quot;&gt;\r\n  &lt;xs:element name=&quot;Person&quot; nillable=&quot;true&quot; type=&quot;Person&quot; \/&gt;\r\n  &lt;xs:complexType name=&quot;Person&quot;&gt;\r\n    &lt;xs:sequence&gt;\r\n      &lt;xs:element minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot; name=&quot;FirstName&quot; type=&quot;xs:string&quot; \/&gt;\r\n      &lt;xs:element minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot; name=&quot;LastName&quot; type=&quot;xs:string&quot; \/&gt;\r\n      &lt;xs:element minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot; name=&quot;Age&quot; type=&quot;xs:int&quot; \/&gt;\r\n    &lt;\/xs:sequence&gt;\r\n  &lt;\/xs:complexType&gt;\r\n&lt;\/xs:schema&gt;\r\n<\/pre>\n<p>The class created (in C#) looks like the following (comments removed)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;System.CodeDom.Compiler.GeneratedCodeAttribute(&quot;xsd&quot;, &quot;4.0.30319.17929&quot;)]\r\n&#x5B;System.SerializableAttribute()]\r\n&#x5B;System.Diagnostics.DebuggerStepThroughAttribute()]\r\n&#x5B;System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;)]\r\n&#x5B;System.Xml.Serialization.XmlRootAttribute(Namespace=&quot;&quot;, IsNullable=true)]\r\npublic partial class Person {\r\n    \r\n    private string firstNameField;\r\n    \r\n    private string lastNameField;\r\n    \r\n    private int ageField;\r\n    \r\n    public string FirstName {\r\n        get {\r\n            return this.firstNameField;\r\n        }\r\n        set {\r\n            this.firstNameField = value;\r\n        }\r\n    }\r\n    \r\n    public string LastName {\r\n        get {\r\n            return this.lastNameField;\r\n        }\r\n        set {\r\n            this.lastNameField = value;\r\n        }\r\n    }\r\n    \r\n    public int Age {\r\n        get {\r\n            return this.ageField;\r\n        }\r\n        set {\r\n            this.ageField = value;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Creating an XML schema based on an XML file<\/strong><\/p>\n<p>It might be that we&#8217;ve got an XML file but no xml schema, so we&#8217;ll need to convert that to an xml schema before we can generate our classes file. Again we can use xsd.exe<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nxsd person.xml\r\n<\/pre>\n<p>the above will create an xml schema based upon the XML file, obviously this is limited to what is available in the XML file itself, so if your XML doesn&#8217;t have &#8220;optional&#8221; elements\/attributes xsd.exe obviously cannot include those in the schema it produces.<\/p>\n<p>Assuming we therefore started with an XML file, the person.xml, which looks like the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n\r\n&lt;Person&gt;\r\n   &lt;FirstName&gt;Spoungebob&lt;\/FirstName&gt;\r\n   &lt;LastName&gt;Squarepants&lt;\/LastName&gt;\r\n   &lt;Age&gt;21&lt;\/Age&gt;\r\n&lt;\/Person&gt;\r\n<\/pre>\n<p><em>Note: I&#8217;ve no idea if that is really SpongeBob&#8217;s age.<\/em><\/p>\n<p>Running xsd.exe against person.xml file we get the following xsd schema<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;xs:schema id=&quot;NewDataSet&quot; xmlns=&quot;&quot; xmlns:xs=&quot;http:\/\/www.w3.org\/2001\/XMLSchema&quot; xmlns:msdata=&quot;urn:schemas-microsoft-com:xml-msdata&quot;&gt;\r\n  &lt;xs:element name=&quot;Person&quot;&gt;\r\n    &lt;xs:complexType&gt;\r\n      &lt;xs:sequence&gt;\r\n        &lt;xs:element name=&quot;FirstName&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; \/&gt;\r\n        &lt;xs:element name=&quot;LastName&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; \/&gt;\r\n        &lt;xs:element name=&quot;Age&quot; type=&quot;xs:string&quot; minOccurs=&quot;0&quot; \/&gt;\r\n      &lt;\/xs:sequence&gt;\r\n    &lt;\/xs:complexType&gt;\r\n  &lt;\/xs:element&gt;\r\n  &lt;xs:element name=&quot;NewDataSet&quot; msdata:IsDataSet=&quot;true&quot; msdata:UseCurrentLocale=&quot;true&quot;&gt;\r\n    &lt;xs:complexType&gt;\r\n      &lt;xs:choice minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;&gt;\r\n        &lt;xs:element ref=&quot;Person&quot; \/&gt;\r\n      &lt;\/xs:choice&gt;\r\n    &lt;\/xs:complexType&gt;\r\n  &lt;\/xs:element&gt;\r\n&lt;\/xs:schema&gt;\r\n<\/pre>\n<p>From this we could now create our classes as previously outlined.<\/p>\n<p><strong>Creating an xml schema based on .NET type<\/strong><\/p>\n<p>What if we&#8217;ve got a class\/type and we want to serialize it as XML, let&#8217;s use xsd.exe to create the XML schema for us.<\/p>\n<p>If the class looks like the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class Person\r\n{\r\n   public string FirstName { get; set; }\r\n   public string LastName { get; set; }\r\n   public int Age { get; set; }\r\n}\r\n&#x5B;code]\r\n\r\n&lt;em&gt;Note: Assuming the class is compiled into an assembly call DomainObjects.dll&lt;\/em&gt;\r\n\r\nThen running xsd.exe with the following command line\r\n\r\n&#x5B;code language=&quot;xml&quot;]\r\nxsd.exe DomainObjects.dll \/type:Person\r\n<\/pre>\n<p>will then generate the following xml schema<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;xs:schema elementFormDefault=&quot;qualified&quot; xmlns:xs=&quot;http:\/\/www.w3.org\/2001\/XMLSchema&quot;&gt;\r\n  &lt;xs:element name=&quot;Person&quot; nillable=&quot;true&quot; type=&quot;Person&quot; \/&gt;\r\n  &lt;xs:complexType name=&quot;Person&quot;&gt;\r\n    &lt;xs:sequence&gt;\r\n      &lt;xs:element minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot; name=&quot;FirstName&quot; type=&quot;xs:string&quot; \/&gt;\r\n      &lt;xs:element minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot; name=&quot;LastName&quot; type=&quot;xs:string&quot; \/&gt;\r\n      &lt;xs:element minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot; name=&quot;Age&quot; type=&quot;xs:int&quot; \/&gt;\r\n    &lt;\/xs:sequence&gt;\r\n  &lt;\/xs:complexType&gt;\r\n&lt;\/xs:schema&gt;\r\n<\/pre>\n<p>You&#8217;ll notice this is slightly different from the code generated from the person.xml file. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The XML Schema Definition Tool (xsd.exe) can be used to generate xml schema files from XML and better still C# classes from xml schema files. Creating classes based upon an XML schema file So in it&#8217;s simplest usage we can simply type xsd person.xsd \/classes and this generates C# classes representing the xml schema. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,70],"tags":[],"class_list":["post-1697","post","type-post","status-publish","format-standard","hentry","category-c","category-xml"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1697"}],"version-history":[{"count":18,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1697\/revisions"}],"predecessor-version":[{"id":1716,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1697\/revisions\/1716"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}