private void button1_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);
  }

 

生成的XSD为:

<?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> 

相关文章:

  • 2021-09-27
  • 2022-12-23
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2022-01-20
猜你喜欢
  • 2021-09-30
  • 2022-12-23
  • 2022-02-03
  • 2022-03-07
  • 2022-12-23
  • 2021-08-04
  • 2021-06-07
相关资源
相似解决方案