许明会的计算机技术主页

Language:C,C++,.NET Framework(C#)
Thinking:Design Pattern,Algorithm,WPF,Windows Internals
Database:SQLServer,Oracle,MySQL,PostSQL
IT:MCITP,Exchange,Lync,Virtualization,CCNP

导航

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);

        }
    };
}

posted on 2007-12-13 21:38  许明会  阅读(359)  评论(0编辑  收藏  举报