六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

<转>使用XML模式定义DateSet,并用C#显示DateSet内容

[复制链接]

升级  14.67%

18

主题

18

主题

18

主题

秀才

Rank: 2

积分
72
 楼主| 发表于 2013-2-4 20:01:07 | 显示全部楼层 |阅读模式
使用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文档);就可以了
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表