XML和关系数据----将数据集映射保存到XSD架构文件
如何将数据集映射保存到 XSD 架构文件
此示例阐释如何将内部数据集 (DataSet) 映射保存到 XML 架构定义语言 (XSD) 架构文件中。此示例通过使用数据集上的关系方法生成映射以创建表和列。然后,该示例将这些映射的 XSD 架构表示形式写到文件中。
[运行示例] | [查看源代码] |
DataSet 和 XmlDataDocument 类都表示内存中的数据缓存。DataSet 提供面向关系的导航和编辑方法,而 XmlDataDocument 提供 XML 导航和编辑方法。
此示例从 XmlDataDocument 获取 DataSet 属性,使用该属性生成一组表和列,然后填充这些表和列。然后,该示例写出内部生成的架构。
下列示例代码生成两个表,一个用于人,另一个用于宠物。该示例使用 ID 作为每个表的主关键字,并建立人和他们的宠物之间的关系表。为此,该示例首先创建 XmlDataDocument 的一个实例,然后将与 XmlDataDocument 关联的数据集传递给 LoadDataSet 方法。
XmlDataDocument datadoc = new XmlDataDocument(); LoadDataSet(datadoc.DataSet); |
||
C# | VB |
LoadDataSet 方法使用关系数据加载数据集。
// Load a DataSet with relational data private void LoadDataSet(DataSet dataset) { try { Console.WriteLine("Loading the DataSet ..."); // Set DataSet name dataset.DataSetName = "PersonPet"; // Create tables for people and pets DataTable people = new DataTable("Person"); DataTable pets = new DataTable("Pet"); // Set up the columns in the Tables DataColumn personname = new DataColumn ("Name", typeof(String)); DataColumn personAge = new DataColumn ("Age", typeof(Int32)); DataColumn petname = new DataColumn ("Name", typeof(String)); DataColumn pettype = new DataColumn ("Type", typeof(String)); // Add columns to person table DataColumn id = people.Columns.Add("ID", typeof(Int32)); id.AutoIncrement = true; people.PrimaryKey = new DataColumn[] {id}; people.Columns.Add (personname); people.Columns.Add (personAge); // Add columns to pet table id = pets.Columns.Add("ID", typeof(Int32)); id.AutoIncrement = true; pets.PrimaryKey = new DataColumn[] {id}; id.AutoIncrement = true; DataColumn ownerid = pets.Columns.Add("OwnerID", typeof(Int32)); DataColumn[] foreignkey = new DataColumn[] {ownerid}; pets.Columns.Add (petname); pets.Columns.Add (pettype); // Add tables to the DataSet dataset.Tables.Add (people); dataset.Tables.Add (pets); // Add people DataRow mark = people.NewRow(); mark[personname] = "Mark"; mark[personAge] = 18; people.Rows.Add(mark); DataRow william = people.NewRow(); william[personname] = "William"; william[personAge] = 12; people.Rows.Add(william); DataRow james = people.NewRow(); james[personname] = "James"; james[personAge] = 7; people.Rows.Add(james); DataRow levi = people.NewRow(); levi[personname] = "Levi"; levi[personAge] = 4; people.Rows.Add(levi); // Add relationships Console.WriteLine("Creating relationships between people and pets ..."); DataRelation personpetrel = new DataRelation ("PersonPet",people.PrimaryKey, foreignkey); dataset.Relations.Add (personpetrel); // Add pets DataRow row = pets.NewRow(); row["OwnerID"] = mark["ID"]; row[petname] = "Frank"; row[pettype] = "cat"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = william["ID"]; row[petname] = "Rex"; row[pettype] = "dog"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = james["ID"]; row[petname] = "Cottontail"; row[pettype] = "rabbit"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = levi["ID"]; row[petname] = "Sid"; row[pettype] = "snake"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = levi["ID"]; row[petname] = "Tickles"; row[pettype] = "spider"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = william["ID"]; row[petname] = "Tweetie"; row[pettype] = "canary"; pets.Rows.Add(row); // commit changes dataset.AcceptChanges(); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } } |
||
C# | VB |
DataSet 的 AcceptChanges 方法接受所有的更改,这些更改是自加载数据集以来或自上次调用 AcceptChanges 以来对数据集所做的更改。所有新的和修改过的行保持不变,移除已删除的行。有关其他 DataSet 关系方法的更多信息,请参阅如何获取 ADO.NET 的概述
为了将架构保存到文件,该示例调用 DataSet 的 WriteXmlSchema 方法,传递表示目标文件的 StreamWriter 类。
StreamWriter writer = null; try { Console.WriteLine("Writing the schema to {0} ...", mySaveSchema); writer = new StreamWriter(mySaveSchema); datadoc.DataSet.WriteXmlSchema(writer); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } finally { if (writer != null) writer.Close(); } |
||
C# | VB |
下列输出显示由 DisplayTables 方法在数据集中创建的表。有关 DisplayTables 方法的更多信息,请参阅如何从 XML 推导出数据集映射。该示例将推导出的架构写到 PersonPet.xsd 文件中。
Loading the DataSet ... Creating relationships between people and pets ... DataSet: PersonPet contains ... No of Tables: 2 Table content ... TableName = Person --------- Columns ... ID Name Age Number of rows = 4 Rows ... 0 Mark 18 1 William 12 2 James 19 3 Levi 4 TableName = Pet --------- Columns ... ID OwnerID Name Type Number of rows = 6 Rows ... 0 0 Frank cat 1 1 Rex dog 2 2 Cottontail rabbit 3 3 Sid snake 4 3 Tickles spider 5 1 Tweetie canary PersonPet Name = Mark owns Pet = Frank the cat Name = William owns Pet = Rex the dog Pet = Tweetie the canary Name = James owns Pet = Cottontail the rabbit Name = Levi owns Pet = Sid the snake Pet = Tickles the spider Writing the schema to PersonPet.xsd ...
摘要
- WriteXmlSchema 方法将数据集中的关系数据的内部结构映射保存为 XSD 架构。
- XmlDataDocument 的 DataSet 属性使您能够相关地查看和管理 XML 文档中的结构化数据