1. 程式人生 > 其它 >【C# XML 】XML Shcema 案例

【C# XML 】XML Shcema 案例

框架

<?xml version="1.0" encoding="utf-8"?>
<schema id="Stocks"
    targetNamespace="http://tempuri.org/Stocks"
    elementFormDefault="qualified"
    xmlns:this="http://tempuri.org/Stocks"
    xmlns="http://www.w3.org/2001/XMLSchema"
           
>

    <!--=====================================定義k線 Type=========================================-->
    
    <!--定義簡易元素-->
    <element name="
OpenPrice" type="this:PriceRang" /> <element name="ClosePrice" type="this:PriceRang"/> <element name="HightPrice" type="this:PriceRang"/> <element name ="LowPrice" type="this:PriceRang"/> <element name="KLine" type="this:OCHLElement" /> <!--定義一個型別 表示股價的範圍--> <simpleType name="
PriceRang"> <restriction base="decimal"> <minInclusive value ="0"></minInclusive> <maxExclusive value="100000000000"></maxExclusive> </restriction> </simpleType> <!--定義K線複合元素--> <complexType name="OCHLElement
" mixed="true" > <sequence > <element ref="this:OpenPrice" maxOccurs="1"/> <element ref="this:ClosePrice" maxOccurs="1"/> <element ref="this:HightPrice" maxOccurs="1"/> <element ref="this:LowPrice" maxOccurs="1"/> </sequence> </complexType> <!--==============================================================================--> <!--根元素--> <element name ="Stock" type="this:stock" /> <!--定義公司名稱 範圍--> <complexType name="stock" > <sequence > <element ref="this:Company" maxOccurs="1"/> <element ref="this:KLine" maxOccurs="unbounded"/> </sequence> </complexType> <!--根元素--> <element name ="Company" type="this:CompanyNameRang" /> <!--定義公司名稱 範圍--> <simpleType name="CompanyNameRang"> <restriction base="string"> <pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]+"/> </restriction> </simpleType> </schema>

框架解釋

建立一個k線 框架。

例項

用已經定義的Stock框架 ,建立例項文件

<?xml version="1.0" encoding="utf-8" ?>
<Stock xmlns="http://tempuri.org/Stocks">
    <Company>tengXun</Company>

    <KLine>
        <OpenPrice>15</OpenPrice>
        <ClosePrice>15.54</ClosePrice>
        <HightPrice>15.90</HightPrice>
        <LowPrice>14.90</LowPrice>
        
    </KLine>
    <KLine>
        <OpenPrice>16</OpenPrice>
        <ClosePrice>17.54</ClosePrice>
        <HightPrice>17.54</HightPrice>
        <LowPrice>15.90</LowPrice>

    </KLine>
</Stock>