C#生成XSD规范,利用XmlSchema类

1。xsd基础:

类型:xs:integer;   xs:positiveInteger;(>0的整数);    xs:nonPositiveInteger;(<=0的整数);
        xs:Bool;    xs:string
        xs:dateTime;(日+时);       xs:date;(日);           xs:time;(时);

<xs:schema....>

<xs:complexType name="autotype">------2级
   <xs:sequence>
         <xs:element name="name" type="xs:string"/>-----1级
    </xs:sequence>
</xs:complexType>

<xs:complexType name="booktype">-----3级
   <xs:sequence> 
         <xs:element name="typename" type="autotype"/>------应用2级
    </xs:sequence>
</xs:complexType>

<xs:element name="book" type="booktype"/>-----应用3级

</xs:schema>

2。设计成XML模式

class Program
    {
        [STAThread]
        static void Main(string[] args)
        { 
XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
            nsm.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
            XmlSchema sche = new XmlSchema();
            XmlSchemaComplexType cauth = new XmlSchemaComplexType();
            cauth.Name = "author";
            XmlSchemaSequence seqauth = new XmlSchemaSequence();
            XmlSchemaElement ele = new XmlSchemaElement();
            ele.Name = "name";
            ele.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
            seqauth.Items.Add(ele);
            XmlSchemaElement eleage = new XmlSchemaElement();
            eleage.Name = "age";
            eleage.SchemaTypeName = new XmlQualifiedName("positiveInteger", "http://www.w3.org/2001/XMLSchema");
            seqauth.Items.Add(eleage);
            cauth.Particle = seqauth;
            sche.Items.Add(cauth);            
            sche.Compile(new ValidationEventHandler(valia));
            sche.Write(Console.Out, nsm);
        }
}

个人总结:

结果:

<?xml version="1.0" encoding="gb2312"?>
----------------xs:..........->xmlNamespaceManager.AddNamespace
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">-new XmlSchema  
<xs:complexType name="author">------------------new XmlSchemaComplexType
    <xs:sequence>----------------------------------new XmlSchemaSequence
      <xs:element name="name" type="xs:string"/>---new XmlSchemaElement
      <xs:element name="age" type="xs:positiveInteger"/>------new XmlSchemaElement 
    </xs:seqence>
  </xs:complexType>
</xs:schema>

 

转自:http://www.cnblogs.com/winvay/articles/1321496.html

posted @ 2012-01-31 09:33  龙行星宇  阅读(440)  评论(0编辑  收藏  举报