现在的位置: 首页 > 综合 > 正文

用程序生成XSD模式.

2012年09月28日 ⁄ 综合 ⁄ 共 1506字 ⁄ 字号 评论关闭
  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> 

抱歉!评论已关闭.