zoukankan      html  css  js  c++  java
  • 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 数据
  • 相关阅读:
    面经 收藏的 这可能不只是一篇面经
    2017网易秋招编程集合
    网易2017春招笔试真题编程题集合题解
    小朋友 排序
    网易编程题目 02 赶去公司
    网易有道2017内推选择题
    12,享元模式(Flyweight Pattern)是以共享的方式高效的支持大量的细粒度的对象。
    11,外观模式(Facade Pattern)是为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
    新手进行Oracle MIS系统开发的步骤
    Oracle常用查看表结构命令(一)
  • 原文地址:https://www.cnblogs.com/chorrysky/p/584521.html
Copyright © 2011-2022 走看看