Private Void Button#click ( Object Sender, system. eventargs e ){
XMLSCHEMA Schema = New XMLSCHEMA ();
Xmlschemaelement elementbook = New Xmlschemaelement ();
Schema. Items. Add (elementbook );
Elementbook. Name = " Book " ;
Xmlschemacomplextype ctype = New Xmlschemacomplextype ();
Elementbook. schematype = Ctype;
Xmlschemasequence Sequence = New Xmlschemasequence ();
Ctype. Particle = Sequence;
Xmlschemaelement elementtitle = New Xmlschemaelement ();
Elementtitle. Name = " Title " ;
Elementtitle. schematypename = New Xmlqualifiedname ( " String " , " Http://www.w3.org/2001/XMLSchema " );
Xmlschemaelement elementpublisher = New Xmlschemaelement ();
Elementpublisher. Name = " Publisher " ;
Elementpublisher. schematypename = New Xmlqualifiedname ( " String " , " Http://www.w3.org/2001/XMLSchema " );
Sequence. Items. Add (elementtitle );
Sequence. Items. Add (elementpublisher );
Schema. Compile ( New Validationeventhandler (validationhandler ));
System. Io. filestream F = New Filestream ( @" C: \ 1.xml " , Filemode. createnew );
Schema. Write (f );
F. Close ();
}
Public Static Void Validationhandler ( Object Sender, validationeventargs ARGs ){
MessageBox. Show ( " Schema validation failed. " );
MessageBox. Show (ARGs. Message );
}
The generated XSD is:
<? XML version = " 1.0 " ?>
< Xs: schema xmlns: XS = " Http://www.w3.org/2001/XMLSchema " >
< Xs: element name = " Book " >
< Xs: complextype >
< Xs: Sequence >
< Xs: element name = " Title " Type = " Xs: String " />
< Xs: element name = " Publisher " Type = " Xs: String " />
</ Xs: Sequence >
</ Xs: complextype >
</ Xs: Element >
</ Xs: Schema >