使用XML模式定義DateSet,並用C#顯示DateSet內容

使用XML模式定義DateSet:test.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schema-microsoft-com:xml-msdata" elementFormDefault="qualified" attributeFormDefault="unqualified" id="mydataset">
 <xs:element name="ComplexType" msdata:IsDataSet="true">
  <xs:complexType>
   <xs:choice maxOccurs="unbounded">

表一:
    <xs:element name="book">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="ISBN" type="xs:string"/>
       <xs:element name="author" type="xs:positiveInteger"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>

表二:
    <xs:element name="author">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="authorid" type="xs:positiveInteger"/>
       <xs:element name="NAME" type="xs:string"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:choice>
  </xs:complexType>

下列爲兩個表的聯繫設置:

設置Key鍵:

從表:
  <xs:key name="authorkey">
   <xs:selector xpath=".//author"/>
   <xs:field xpath="authorid"/>
  </xs:key>

設置keyref形成關係

主表:
  <xs:keyref name="authorref" refer="authorkey">
   <xs:selector xpath=".//book"/>
   <xs:field xpath="author"/>
  </xs:keyref>
 </xs:element>
</xs:schema>
總結:Key->keyref->關係

下面在C#中如何顯示DateSet相關信息

class Program
 {
        [STAThread]
        static void Main(string[] args)
        {
  DataSet ds = new DataSet();
            ds.ReadXmlSchema("test.xsd");
            foreach(DataTable dt in ds.Tables )
            { Console.WriteLine("{0}", dt.TableName);}

            foreach (DataColumn dc in dt.Columns)
            { Console.Write("{0},<{1}>",dc.ColumnName,dc.DataType.ToString());}

顯示:
book
ISBN,<System.String>author,<System.UInt64>
author
authorid,<System.UInt64>NAME,<System.String>


            Console.WriteLine("\n");
            }

 

            foreach (DataRelation dr in ds.Relations)
            { Console.WriteLine("{0},{1}",dr.RelationName,dr.Nested.ToString());}

顯示:----authorref,False
         }
}

總結:DateSet ReadXmlSchema(具有DateSet描述的xsd文檔);就可以了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章