본문 바로가기
C#(CSharp)/Etc

XSD <-> XML 상호 관계

by swconsulting 2015. 3. 17.

예전에는 XSD를 거의 사용하지 않고 XML을 사용했다. 하지만 지난 프로젝트에 서버 클라이언트 통신을 하면서 XML을 이용했다. 

이후 XSD 중요성을 깨달았다.


[XSD]

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

<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"

    elementFormDefault="qualified"

    xmlns="http://tempuri.org/XMLSchema.xsd"

    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"

    xmlns:xs="http://www.w3.org/2001/XMLSchema"

> 

 

  <xs:annotation>

    <xs:documentation xml:lang="en">

      SWConsulting Service Korea Sticky Note App (SWConsulting.blog.me)

      Copyright 2015 SWConsulting Service Korea. All rights reserved.

    </xs:documentation>

  </xs:annotation>

 

  <xs:element name="stickyNoteConfig" type="StickyNoteConfigInfo"/>

 

  <xs:element name="comment" type="xs:string"/>

 

  <xs:complexType name="StickyNoteConfigInfo">

    <xs:sequence>

      <xs:element ref="comment" minOccurs="0"/>

      <xs:element name="config" type="Config" />

      <xs:element name="address" type="Address" minOccurs="0"/>           

      <xs:element name="noteInfos"  type="NoteInfos"/>

    </xs:sequence>

    <xs:attribute name="buildDate" type="xs:date"/>

  </xs:complexType>

 

  <xs:complexType name="Config">

    <xs:sequence>

      <xs:element name="userName"   type="xs:string"/>

      <xs:element name="email" type="xs:string"/>

      <xs:element name="useEmail"   type="xs:boolean"/>

      <xs:element name="alwaysRun"  type="xs:boolean"/>     

    </xs:sequence>

  </xs:complexType>

 

  <xs:complexType name="Address">

    <xs:annotation>

      <xs:documentation>

        SWConsulting Service Korea Sticky Note App (SWConsulting.blog.me)

        Copyright 2015 SWConsulting Service Korea. All rights reserved.

      </xs:documentation>

      <xs:appinfo>

        Application info.

      </xs:appinfo>

    </xs:annotation>

 

    <xs:sequence>

      <xs:element name="name"   type="xs:string"/>

      <xs:element name="street" type="xs:string"/>

      <xs:element name="city"   type="xs:string"/>

      <xs:element name="state"  type="xs:string"/>

      <xs:element name="postCode"    type="xs:decimal"/>

    </xs:sequence>

    <xs:attribute name="country" type="xs:string"

                  fixed="KR"/>

  </xs:complexType>

 

  <xs:complexType name="NoteInfos">

    <xs:sequence>

      <xs:element name="note" minOccurs="0" maxOccurs="unbounded">

        <xs:complexType>

          <xs:sequence>

            <xs:element name="productName" type="xs:string"/>

            <xs:element name="modifyCount">

              <xs:simpleType>

                <xs:restriction base="xs:positiveInteger">

                  <xs:maxExclusive value="999"/>

                </xs:restriction>

              </xs:simpleType>

            </xs:element>

            <xs:element name="delYN"    type="xs:boolean"/>

            <xs:element ref="comment"   minOccurs="0"/>

            <xs:element name="writeDate" type="xs:date" minOccurs="0"/>

          </xs:sequence>

          <xs:attribute name="noteId" type="NKU" use="required"/>

        </xs:complexType>

      </xs:element>

    </xs:sequence>

  </xs:complexType>

 

  <!-- Note Keeping Id, a code for identifying notes -->

  <xs:simpleType name="NKU">

    <xs:restriction base="xs:string">

      <xs:pattern value="\d{5}-[a-z]{3}"/>

    </xs:restriction>

  </xs:simpleType

 

 

</xs:schema>

 

 

[XML]

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

<stickyNoteConfig buildDate="1900-01-01" xmlns="http://tempuri.org/XMLSchema.xsd">

  <comment>comment1</comment>

  <config>

    <userName>userName1</userName>

    <email>email1</email>

    <useEmail>true</useEmail>

    <alwaysRun>true</alwaysRun>

  </config>

  <address country="KR">

    <name>name1</name>

    <street>street1</street>

    <city>city1</city>

    <state>state1</state>

    <postCode>1</postCode>

  </address>

  <noteInfos>

    <note noteId="12345-abc">

      <productName>productName1</productName>

      <modifyCount>1</modifyCount>

      <delYN>true</delYN>

      <comment>comment1</comment>

      <writeDate>1900-01-01</writeDate>

    </note>

    <note noteId="12345-abc">

      <productName>productName2</productName>

      <modifyCount>998</modifyCount>

      <delYN>false</delYN>

      <comment>comment2</comment>

      <writeDate>0001-01-01</writeDate>

    </note>

    <note noteId="12345-abc">

      <productName>productName3</productName>

      <modifyCount>2</modifyCount>

      <delYN>true</delYN>

      <comment>comment3</comment>

      <writeDate>9999-12-31</writeDate>

    </note>

  </noteInfos>

 

</stickyNoteConfig>



[XSD convert to class]


Visual Studio Tool (Visual Studio Tool Command)

xsd /c YourFile.xsd



출처 : 다년간의 프로그래밍 경험