使用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" >
<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文档);就可以了;
|
请发表评论