使用xsd.exe命令 根据指定的xml文件生成对应的xsd架构文件

打开vs命令窗口,如图:

image

输入命令:xsd file.xml [/outputdir:directory] [/parameters:file.xml]

例:

image

附文件一:Categories.xml

<?xml version="1.0" encoding="utf-8" ?>
<categories>
  <category>
    <id>2</id>
    <visible>true</visible>
    <title>新闻分类</title>
    <description></description>
    <parentId></parentId>
  </category>  
  <category>
    <id>0001</id>
    <visible>true</visible>
    <title>新闻分类一</title>
    <description>测试一~</description>
    <parentId>2</parentId>
  </category>
  <category>
    <id>0002</id>
    <visible>true</visible>
    <title>新闻分类二</title>
    <description>测试二~</description>
    <parentId>2</parentId>
  </category>
</categories>
附文件二:转化后的Categories.xsd
注意:转化后可能要对文件内容稍作修改,转化后的文件中element的type属性默认为“xs:string”
这里根据实际情况将visible的type属性修改为“xs:boolean
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="categories" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="categories" msdata:IsDataSet="true" msdata:Locale="en-US">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="category">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="id" type="xs:string" minOccurs="0" />
              <xs:element name="visible" type="xs:boolean" minOccurs="0" />  <!-- 注意 -->
              <xs:element name="title" type="xs:string" minOccurs="0" />
              <xs:element name="description" type="xs:string" minOccurs="0" />
              <xs:element name="parentId" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

 

---------

备注:如果不通过xml文件自动生成,也可以通过“新建 xml schema文件”创建新的xsd文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema"
    targetNamespace="http://tempuri.org/XMLSchema.xsd" <!-- 这句代码需要删除,否则通过DataSet读取xml数据时读不出来,为什么?去掉就可以显示 -->
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
</xs:schema>

 

 

posted on 2010-02-25 17:56  lihua好心情  阅读(831)  评论(0编辑  收藏  举报