Last active
August 29, 2015 14:25
-
-
Save jpluimers/4247df4e0aa01c1c190a to your computer and use it in GitHub Desktop.
Example of xsd2code only handling xsd annotations for attributes, not for elements, types and other places where they can be used in an XSD. Steps to reproduce: 1. Install xsd2code and Visual Studio. 2. Put all these files in one directory. 3. Run `generate-C#-from-XSD-annotations.bat`. 4. Diff `annotations.xsd.exe.cs` and `annotations.xsd2code.…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" | |
xmlns:example="http://example.org/annotations" | |
targetNamespace="http://example.org/annotations" | |
elementFormDefault="qualified"> | |
<xs:annotation> | |
<xs:documentation> | |
Demonstration of xs:annotation xs:documentation blocks to appear in xsd2code generated C# code. | |
Note: | |
- xsd.exe does not support annotations. Tested with NETFX 4.5.1 Tools. | |
- xsd2code only for attributes. Teste with version 3.4.0.32990. | |
</xs:documentation> | |
</xs:annotation> | |
<xs:element name="root" type="example:root"> | |
<xs:annotation> | |
<xs:documentation>Information root.</xs:documentation> | |
</xs:annotation> | |
</xs:element> | |
<xs:complexType name="root"> | |
<xs:annotation> | |
<xs:documentation>Complex root type.</xs:documentation> | |
</xs:annotation> | |
<xs:all> | |
<xs:annotation> | |
<xs:documentation> | |
Elements in any order. | |
</xs:documentation> | |
</xs:annotation> | |
<xs:element name="TestElement" type="example:string50"> | |
<xs:annotation> | |
<xs:documentation> | |
Test data in element. | |
</xs:documentation> | |
</xs:annotation> | |
</xs:element> | |
</xs:all> | |
<xs:attribute name="TestAttribute" use="optional" type="example:string50"> | |
<xs:annotation> | |
<xs:documentation> | |
Optional test data in attribute. | |
</xs:documentation> | |
</xs:annotation> | |
</xs:attribute> | |
</xs:complexType> | |
<xs:simpleType name="string50"> | |
<xs:restriction base="xs:string"> | |
<xs:minLength value="1"/> | |
<xs:maxLength value="50"/> | |
</xs:restriction> | |
</xs:simpleType> | |
<xs:annotation> | |
<xs:documentation> | |
End of the XSD. | |
</xs:documentation> | |
</xs:annotation> | |
</xs:schema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------ | |
// <auto-generated> | |
// This code was generated by a tool. | |
// Runtime Version:4.0.30319.18449 | |
// | |
// Changes to this file may cause incorrect behavior and will be lost if | |
// the code is regenerated. | |
// </auto-generated> | |
//------------------------------------------------------------------------------ | |
// | |
// This source code was auto-generated by xsd, Version=4.0.30319.17929. | |
// | |
namespace annotations { | |
using System.Xml.Serialization; | |
/// <remarks/> | |
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")] | |
[System.SerializableAttribute()] | |
[System.Diagnostics.DebuggerStepThroughAttribute()] | |
[System.ComponentModel.DesignerCategoryAttribute("code")] | |
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/annotations")] | |
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://example.org/annotations", IsNullable=false)] | |
public partial class root { | |
private string testElementField; | |
private string testAttributeField; | |
/// <remarks/> | |
public string TestElement { | |
get { | |
return this.testElementField; | |
} | |
set { | |
this.testElementField = value; | |
} | |
} | |
/// <remarks/> | |
[System.Xml.Serialization.XmlAttributeAttribute()] | |
public string TestAttribute { | |
get { | |
return this.testAttributeField; | |
} | |
set { | |
this.testAttributeField = value; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------------------------------------------------------------------------------ | |
// <auto-generated> | |
// Generated by Xsd2Code. Version 3.4.0.32989 | |
// <NameSpace>Annotations</NameSpace><Collection>List</Collection><codeType>CSharp</codeType><EnableDataBinding>False</EnableDataBinding><EnableLazyLoading>False</EnableLazyLoading><TrackingChangesEnable>False</TrackingChangesEnable><GenTrackingClasses>False</GenTrackingClasses><HidePrivateFieldInIDE>False</HidePrivateFieldInIDE><EnableSummaryComment>True</EnableSummaryComment><VirtualProp>False</VirtualProp><IncludeSerializeMethod>False</IncludeSerializeMethod><UseBaseClass>False</UseBaseClass><GenBaseClass>False</GenBaseClass><GenerateCloneMethod>False</GenerateCloneMethod><GenerateDataContracts>False</GenerateDataContracts><CodeBaseTag>Net20</CodeBaseTag><SerializeMethodName>Serialize</SerializeMethodName><DeserializeMethodName>Deserialize</DeserializeMethodName><SaveToFileMethodName>SaveToFile</SaveToFileMethodName><LoadFromFileMethodName>LoadFromFile</LoadFromFileMethodName><GenerateXMLAttributes>True</GenerateXMLAttributes><OrderXMLAttrib>False</OrderXMLAttrib><EnableEncoding>False</EnableEncoding><AutomaticProperties>False</AutomaticProperties><GenerateShouldSerialize>False</GenerateShouldSerialize><DisableDebug>False</DisableDebug><PropNameSpecified>Default</PropNameSpecified><Encoder>UTF8</Encoder><CustomUsings></CustomUsings><ExcludeIncludedTypes>False</ExcludeIncludedTypes><EnableInitializeFields>True</EnableInitializeFields> | |
// </auto-generated> | |
// ------------------------------------------------------------------------------ | |
namespace Annotations { | |
using System; | |
using System.Diagnostics; | |
using System.Xml.Serialization; | |
using System.Collections; | |
using System.Xml.Schema; | |
using System.ComponentModel; | |
using System.Collections.Generic; | |
/// <summary> | |
/// Information root. | |
/// </summary> | |
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.32990")] | |
[System.SerializableAttribute()] | |
[System.ComponentModel.DesignerCategoryAttribute("code")] | |
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/annotations")] | |
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://example.org/annotations", IsNullable=false)] | |
public partial class root { | |
private string testElementField; | |
private string testAttributeField; | |
public string TestElement { | |
get { | |
return this.testElementField; | |
} | |
set { | |
this.testElementField = value; | |
} | |
} | |
/// <summary> | |
/// Optional test data in attribute. | |
/// </summary> | |
[System.Xml.Serialization.XmlAttributeAttribute()] | |
public string TestAttribute { | |
get { | |
return this.testAttributeField; | |
} | |
set { | |
this.testAttributeField = value; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
call :do call run-xsd.exe.bat /classes annotations.xsd /namespace:annotations | |
copy annotations.cs annotations.xsd.exe.cs | |
del annotations.cs | |
call :xsd2code annotations.xsd Annotations annotations.xsd2code.exe.cs | |
:: prevent "does not contain a static 'Main' method suitable for an entry point": /target:library | |
call run-csc.exe /target:library annotations.xsd.exe.cs | |
call run-csc.exe /target:library annotations.xsd2code.exe.cs | |
goto :eof | |
:xsd2code | |
:: /is+ serialize | |
:: /cl+ Include Clone methods (will generate CS0114 warnings "hides inherited member" when compiling C# and you inherit types in your XSD) | |
:: /xa+ Xml.Serialization attributes: | |
:: /gbc+ Use Generic Base Class | |
:: /eit+ Exclude types from included Schemas in generation (entity framework EntityBase) | |
:: /sc+ Enable Summary Comment | |
:: xsd2code cannot handle relative paths on the command-line nicely | |
call :do "C:\Program Files (x86)\Xsd2Code\Xsd2Code.exe" %* /c List /sc+ /dbg /hp- /xa+ /pl:Net35 | |
goto :eof | |
:do | |
echo %* | |
%* | |
goto :eof |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
:: Dynamically finds the installed csc.exe, then calls it with the passed parameters | |
:: test these environment variables that have 110 or 120 in them (future enhancements: support more Visual Studio versions): | |
:: Visual Studio .NET 2002: VS70COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio .NET\Common7\Tools\ | |
:: Visual Studio .NET 2003: VS71COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio .NET 2003\Common7\Tools\ | |
:: Visual Studio 2005: VS80COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\ | |
:: Visual Studio 2008: VS90COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\ | |
:: Visual Studio 2010: VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\ | |
:: Visual Studio 2012: VS110COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\ | |
:: Visual Studio 2013: VS120COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\ | |
:: They contain `vsvars32.bat` which will update the `PATH` so it includes where `xsd.exe` resides | |
setlocal enabledelayedexpansion | |
:: delayed expansion allows for the exclamation marks | |
:: see http://ss64.com/nt/delayedexpansion.html | |
:: see http://stackoverflow.com/questions/22857407/windows-batch-how-to-assign-variable-with-dynamic-name | |
set target=csc.exe | |
for %%v in (70 71 80 90 100 110 120 130) do if not [!VS%%vCOMNTOOLS!]==[] set VSCOMNTOOLS=!VS%%vCOMNTOOLS! | |
call :do call "!VSCOMNTOOLS!vsvars32.bat" | |
call :do where %target% | |
%target% %* | |
endlocal | |
goto :eof | |
:do | |
echo %* | |
%* | |
goto :eof | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
:: Dynamically finds the installed xsd.exe, then calls it with the passed parameters | |
:: test these environment variables that have 110 or 120 in them (future enhancements: support more Visual Studio versions): | |
:: Visual Studio .NET 2002: VS70COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio .NET\Common7\Tools\ | |
:: Visual Studio .NET 2003: VS71COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio .NET 2003\Common7\Tools\ | |
:: Visual Studio 2005: VS80COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\ | |
:: Visual Studio 2008: VS90COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\ | |
:: Visual Studio 2010: VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\ | |
:: Visual Studio 2012: VS110COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\ | |
:: Visual Studio 2013: VS120COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\ | |
:: They contain `vsvars32.bat` which will update the `PATH` so it includes where `xsd.exe` resides | |
setlocal enabledelayedexpansion | |
:: delayed expansion allows for the exclamation marks | |
:: see http://ss64.com/nt/delayedexpansion.html | |
:: see http://stackoverflow.com/questions/22857407/windows-batch-how-to-assign-variable-with-dynamic-name | |
for %%v in (70 71 80 90 100 110 120 130) do if not [!VS%%vCOMNTOOLS!]==[] set VSCOMNTOOLS=!VS%%vCOMNTOOLS! | |
call :do call "!VSCOMNTOOLS!vsvars32.bat" | |
call :do where xsd.exe | |
xsd.exe %* | |
endlocal | |
goto :eof | |
:do | |
echo %* | |
%* | |
goto :eof | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment