XML和关系数据用XML加载数据集

如何在数据集中加载 XML

此示例阐释如何使用 XML 数据加载数据集 (DataSet)。该示例是根据主题如何从 XSD 架构创建数据集映射建立的,方法是首先将 XML 数据加载到 XmlDataDocument 中,然后从数据集访问这些数据。为了创建内部映射,该数据集已加载了一个架构。下面的示例显示 XML 数据和创建关系对象之间的转换,以访问该 XML 数据。

 
VB LoadDataSetXMLData.aspx

[运行示例] | [查看源代码]

如下列代码所示,此示例首先实现 ParseSchema 函数以将 XML 架构定义 (XSD) 语言架构 books.xsd 加载到 XmlDataDocument 的 DataSet 属性中。然后,此示例使用 XmlDataDocument 的 Load 方法加载 XML 文件 books.xml

private const String document = "books.xml";
            private const String myLoadSchema = "books.xsd";
            private XmlDataDocument myXmlDataDocument;
            public static void Main()
            {
            String[] args = {document, myLoadSchema};
            LoadDataSetXMLDataSample myLoadDataSetXMLDataSample = new LoadDataSetXMLDataSample();
            myLoadDataSetXMLDataSample.Run(args);
            }
            public void Run(String[] args)
            {
            try
            {
            Console.WriteLine("Creating an XmlDataDocument ...");
            myXmlDataDocument = new XmlDataDocument();
            ParseSchema(args[1]);
            DisplayTableStructure();
            myXmlDataDocument.Load(args[0]);
            DisplayTables(myXmlDataDocument.DataSet);
            }
            catch (Exception e)
            {
            Console.WriteLine ("Exception: {0}", e.ToString());
            }
            }
            // Loads a specified schema into the DataSet
            public void ParseSchema(String schema)
            {
            StreamReader myStreamReader = null;
            try
            {
            Console.WriteLine("Reading Schema file ...");
            myStreamReader = new StreamReader(schema);
            myXmlDataDocument.DataSet.ReadXmlSchema(myStreamReader);
            }
            catch (Exception e)
            {
            Console.WriteLine ("Exception: {0}", e.ToString());
            }
            finally
            {
            if (myStreamReader != null)
            myStreamReader.Close();
            }
            }
            
C# VB  

如在如何从 XSD 架构创建数据集映射中说明的那样,只需通过在表、列和行的集合上迭代,然后设置输出格式,DisplayTableStructure 方法(使用 books.xsd 架构文件生成)即可使该示例显示内部表结构。此示例使用 DisplayTables 方法扩展了该概念(如下列代码所示),该方法使示例得以显示 XML 文件的内容。此示例使用 For Each 关键字而非 For 循环来说明重复集合的其他机制。

// Displays the contents of the DataSet tables
            private void DisplayTables(DataSet dataset)
            {
            // Navigate Dataset
            Console.WriteLine("Content of Tables ...\r\n");
            foreach(DataTable table in dataset.Tables)
            {
            Console.WriteLine("TableName = " + table.TableName);
            Console.WriteLine ("{0}", "---------");
            Console.WriteLine("Columns ...\r\n");
            foreach(DataColumn column in table.Columns)
            {
            Console.Write("{0,-22}",column.ColumnName);
            }
            Console.WriteLine();
            Console.WriteLine("\r\nNumber of rows = {0}", table.Rows.Count.ToString());
            Console.WriteLine("Rows ...\r\n");
            foreach(DataRow row in table.Rows)
            {
            foreach(Object value in row.ItemArray)
            {
            Console.Write("{0,-22}",value.ToString());
            }
            Console.WriteLine();
            }
            Console.WriteLine();
            }
            }
            
C# VB  

下列输出显示 books.xml 的表名、列名和行内容,如 DisplayTables 方法显示的那样。

Creating an XmlDataDocument ...
Reading Schema file ...
Table structure
Tables count=3
TableName='bookstore'.
Columns count=1
ColumnName='bookstore_Id', type = System.Int32
TableName='book'.
Columns count=5
ColumnName='title', type = System.String
ColumnName='price', type = System.Decimal
ColumnName='genre', type = System.String
ColumnName='book_Id', type = System.Int32
ColumnName='bookstore_Id', type = System.Int32
TableName='author'.
Columns count=3
ColumnName='first-name', type = System.String
ColumnName='last-name', type = System.String
ColumnName='book_Id', type = System.Int32
Content of Tables ...
TableName = bookstore
---------
Columns ...
bookstore_Id
Number of rows = 1
Rows ...
0
TableName = book
---------
Columns ...
title                 price                 genre                 book_Id               bookstore_Id
Number of rows = 3
Rows ...
The Autobiography of Benjamin Franklin8.99                  autobiography         0                     0
The Confidence Man    11.99                 novel                 1                     0
The Gorgias           9.99                  philosophy            2                     0
TableName = author
---------
Columns ...
first-name            last-name             book_Id
Number of rows = 3
Rows ...
Benjamin              Franklin              0
Herman                Melville              1
Sidas                 Plato                 2

摘要

  1. 可通过 DataSet 属性上的关系方法访问已加载到 XmlDataDocument 中的 XML 数据。
  2. 当通过 XmlDataDocument 的 DataSet 属性输入关系数据时,也可以读取 XML 数据
原文地址:https://www.cnblogs.com/chorrysky/p/584521.html