2010-07-18 29 views

回答

7

xsd文件是XML架構文件read about itSome more here.

一個簡單的例子:

XMLSchema1.xsd

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="Types" 
    targetNamespace="http://tempuri.org/" 
    elementFormDefault="qualified" 
    xmlns="http://tempuri.org/" 
    xmlns:mstns="http://tempuri.org/" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
> 
    <xs:simpleType name="Types"> 
    <xs:annotation> 
     <xs:documentation>.NET types</xs:documentation> 
    </xs:annotation> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="String" /> 
     <xs:enumeration value="Int16" /> 
     <xs:enumeration value="Int32" /> 
     <xs:enumeration value="Int64" /> 
     <xs:enumeration value="DateTime" /> 
     <xs:enumeration value="Double" /> 
    </xs:restriction> 
    </xs:simpleType> 

    <xs:simpleType name="DataSize"> 
    <xs:annotation> 
     <xs:documentation>Number of bytes of the data</xs:documentation> 
    </xs:annotation> 
    <xs:restriction base="xs:int" /> 
    </xs:simpleType> 

    <!-- ... --> 

</xs:schema> 

然後在你的XML文件,你可以使用:

<?xml version="1.0" encoding="utf-8" ?> 

<ValueSet 
    xmlns="http://tempuri.org/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://tempuri.org/ XMLSchema1.xsd"> 

    <Values> 
    <Value Name="Stats" Type="Int32" DataSize="4" /> 
    <Value Name="Time" Type="DateTime" DataSize="4" /> 
    <Value Name="Some" Type="Double" DataSize="4" /> 
    <Value Name="Other" Type="Double" DataSize="4" /> 
    </Values> 

</ValueSet> 
+1

我想要的是將一些自定義標籤或屬性嵌入到XHTML代碼中,而不會導致語法錯誤。有沒有什麼辦法可以將我的標籤「注入」到使用XML模式的''元素中? – JSPDeveloper01 2011-12-22 03:37:47

4

您可以編寫XSD文件自己,手 - 您只需研究構成XML模式的內容,並瞭解如何自己編寫該代碼。谷歌或必應「XML架構教程」應該給你一大堆命中(例如W3Schools XML Schema Tutorial)。

或者你可以使用Visual Studio這樣做:

Example image

  • 打開要在Visual Studio
  • 處理來自XML菜單中的XML文件,選擇Create Schema菜單項

這將從您的XML文件生成一個XML模式。

注意:這是一個很好的起點 - 但它絕不是完美的。特別是對於較小的XML文件,生成過程無法知道很多事情,只需做出某些假設 - 這可能是正確的或可能是錯誤的。您需要確認XML模式文件 - 這就是第一個選項的專有技術非常方便的地方!