В данной статье разберёмся как получить классы С# из схемы xsd.
Для примера возьмём следующую простую схему:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstName" type="xs:string"/> <xs:element name="lastName" type="xs:string"/> <xs:element name="work" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="company" type="xs:string"/> <xs:element name="position" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Для генерации используйте консоль (cmd.exe) и утилиту xsd.exe:
xsd schema.xsd /classes
Если необходимо сгенерировать классы из нескольких схем, либо есть связанные друг с другом схемы, то просто перечислите их через пробел.
Если в переменной окружения не описан путь к данной утилите, то вы получите следующую ошибку:
‘xsd’ is not recognized as an internal or external command, operable program or batch file.
В этом случае вы можете использовать полный путь для вызова xsd.exe. Данная утилита находится по следующему пути:
C:\Program Files (x86)\Microsoft SDKs\Windows\[Версия]\bin\
Результат:
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System.Xml.Serialization; // // This source code was auto-generated by xsd, Version=4.6.1055.0. // /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] public partial class person { private string firstNameField; private string lastNameField; private personWork workField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string firstName { get { return this.firstNameField; } set { this.firstNameField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string lastName { get { return this.lastNameField; } set { this.lastNameField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public personWork work { get { return this.workField; } set { this.workField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class personWork { private string companyField; private string positionField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string company { get { return this.companyField; } set { this.companyField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string position { get { return this.positionField; } set { this.positionField = value; } } }
С вопросами обращайтесь в комментарии.