XmlSchema:采用编码实现

/*--===------------------------------------------===---
XmlSchema:采用编码实现
            许明会    2007年12月13日 21:37:23
--===------------------------------------------===---
*/
using System;
using System.Xml;
using System.Xml.Schema;

namespace xumh
{
    
public class runMyApp
    {
        
static void Main()
        {
            
//准备XmlSchema
            XmlSchema schema = new XmlSchema();
            
//准备复杂类型元素Book
            XmlSchemaElement eleBook = new XmlSchemaElement();
            eleBook.Name 
= "Book";
            XmlSchemaComplexType type 
= new XmlSchemaComplexType();
            XmlSchemaSequence seq 
= new XmlSchemaSequence();
            type.Particle 
=  seq;
            eleBook.SchemaType 
= type;
            schema.Items.Add(eleBook);

            
//添加元素Title
            XmlSchemaElement eleTitle = new XmlSchemaElement();
            eleTitle.Name 
= "Title";
            eleTitle.SchemaTypeName 
= new XmlQualifiedName("string",
                
"http://www.w3.org/2001/XMLSchema");

            
//添加元素Publisher
            XmlSchemaElement elePub = new XmlSchemaElement();
            elePub.Name 
= "Publisher";
            elePub.SchemaTypeName 
= new XmlQualifiedName("string",
                
"http://www.w3.org/2001/XMLSchema");
            
            seq.Items.Add(eleTitle);
            seq.Items.Add(elePub);

            
//验证可靠性
            schema.Compile(new ValidationEventHandler(ValidationHander));
            System.IO.StreamWriter sw
= new System.IO.StreamWriter(@".\out.xsd");
            schema.Write(sw.BaseStream);
        }
        
static void ValidationHander(object sender, ValidationEventArgs e)
        {
            Console.WriteLine(
"架构验证失败!");
            Console.WriteLine(e.Message);

        }
    };
}
原文地址:https://www.cnblogs.com/flaaash/p/994170.html