当前位置: 首页>>代码示例>>C#>>正文


C# DataSet.ReadXml方法代码示例

本文整理汇总了C#中System.Data.DataSet.ReadXml方法的典型用法代码示例。如果您正苦于以下问题:C# DataSet.ReadXml方法的具体用法?C# DataSet.ReadXml怎么用?C# DataSet.ReadXml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Data.DataSet的用法示例。


在下文中一共展示了DataSet.ReadXml方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DemonstrateReadWriteXMLDocumentWithStreamReader

private void DemonstrateReadWriteXMLDocumentWithStreamReader()
   {
       // Create a DataSet with one table and two columns.
       DataSet OriginalDataSet = new DataSet("dataSet");
       OriginalDataSet.Namespace= "NetFrameWork";
       DataTable table = new DataTable("table");
       DataColumn idColumn = new DataColumn("id", 
           Type.GetType("System.Int32"));
       idColumn.AutoIncrement= true;

       DataColumn itemColumn = new DataColumn("item");
       table.Columns.Add(idColumn);
       table.Columns.Add(itemColumn);
       OriginalDataSet.Tables.Add(table);

       // Add ten rows.
       DataRow newRow;
       for(int i = 0; i < 10; i++)
       {
           newRow = table.NewRow();
           newRow["item"]= "item " + i;
           table.Rows.Add(newRow);
       }
       OriginalDataSet.AcceptChanges();

       // Print out values of each table in the DataSet 
       // using the function defined below.
       PrintValues(OriginalDataSet, "Original DataSet");

       // Write the schema and data to an XML file.
       string xmlFilename = "XmlDocument.xml";

       // Use WriteXml to write the document.
       OriginalDataSet.WriteXml(xmlFilename);

       // Dispose of the original DataSet.
       OriginalDataSet.Dispose();

       // Create a new DataSet.
       DataSet newDataSet = new DataSet("New DataSet");

       // Read the XML document into the DataSet.
       newDataSet.ReadXml(xmlFilename);

       // Print out values of each table in the DataSet 
       // using the function defined below.
       PrintValues(newDataSet,"New DataSet");
   }

   private void PrintValues(DataSet dataSet, string label)
   {
       Console.WriteLine("\n" + label);
       foreach(DataTable table in dataSet.Tables)
       {
           Console.WriteLine("TableName: " + table.TableName);
           foreach(DataRow row in table.Rows)
           {
               foreach(DataColumn column in table.Columns)
               {
                   Console.Write("\table " + row[column] );
               }
               Console.WriteLine();
           }
       }
   }
开发者ID:.NET开发者,项目名称:System.Data,代码行数:65,代码来源:DataSet.ReadXml

示例2: DemonstrateReadWriteXMLDocumentWithFileStream

private void DemonstrateReadWriteXMLDocumentWithFileStream()
   {
       // Create a DataSet with one table and two columns.
       DataSet originalDataSet = new DataSet("dataSet");
       DataTable table = new DataTable("table");
       DataColumn idColumn = new DataColumn("id", 
           Type.GetType("System.Int32"));
       idColumn.AutoIncrement= true;

       DataColumn itemColumn = new DataColumn("item");
       table.Columns.Add(idColumn);
       table.Columns.Add(itemColumn);
       originalDataSet.Tables.Add(table);
       // Add ten rows.

       DataRow newRow;
       for(int i = 0; i < 10; i++)
       {
           newRow = table.NewRow();
           newRow["item"]= "item " + i;
           table.Rows.Add(newRow);
       }
       originalDataSet.AcceptChanges();

       // Print out values of each table in the DataSet  
       // using the function defined below.
       PrintValues(originalDataSet, "Original DataSet");

       // Write the schema and data to XML file with FileStream.
       string xmlFilename = "XmlDocument.xml";
       System.IO.FileStream streamWrite = new System.IO.FileStream
           (xmlFilename, System.IO.FileMode.Create);

       // Use WriteXml to write the XML document.
       originalDataSet.WriteXml(streamWrite);

       // Close the FileStream.
       streamWrite.Close();
      
       // Dispose of the original DataSet.
       originalDataSet.Dispose();
       // Create a new DataSet.
       DataSet newDataSet = new DataSet("New DataSet");
      
       // Read the XML document back in. 
       // Create new FileStream to read schema with.
       System.IO.FileStream streamRead = new System.IO.FileStream
           (xmlFilename,System.IO.FileMode.Open);
       newDataSet.ReadXml(streamRead);

       // Print out values of each table in the DataSet 
       // using the function defined below.
       PrintValues(newDataSet,"New DataSet");
   }

   private void PrintValues(DataSet dataSet, string label)
   {
       Console.WriteLine("\n" + label);
       foreach(DataTable table in dataSet.Tables)
       {
           Console.WriteLine("TableName: " + table.TableName);
           foreach(DataRow row in table.Rows)
           {
               foreach(DataColumn column in table.Columns)
               {
                   Console.Write("\table " + row[column] );
               }
               Console.WriteLine();
           }
       }
   }
开发者ID:.NET开发者,项目名称:System.Data,代码行数:71,代码来源:DataSet.ReadXml

示例3: DemonstrateReadWriteXMLDocumentWithXMLReader

private void DemonstrateReadWriteXMLDocumentWithXMLReader()
   {
       // Create a DataSet with one table and two columns.
       DataSet OriginalDataSet = new DataSet("dataSet");
       OriginalDataSet.Namespace= "NetFrameWork";
       DataTable table = new DataTable("table");
       DataColumn idColumn = new DataColumn("id", 
           Type.GetType("System.Int32"));
       idColumn.AutoIncrement= true;

       DataColumn itemColumn = new DataColumn("item");
       table.Columns.Add(idColumn);
       table.Columns.Add(itemColumn);
       OriginalDataSet.Tables.Add(table);

       // Add ten rows.
       DataRow newRow;
       for(int i = 0; i < 10; i++)
       {
           newRow = table.NewRow();
           newRow["item"]= "item " + i;
           table.Rows.Add(newRow);
       }
       OriginalDataSet.AcceptChanges();

       // Print out values of each table in the DataSet 
       // using the function defined below.
       PrintValues(OriginalDataSet, "Original DataSet");

       // Write the XML schema and data to file with FileStream.
       string xmlFilename = "XmlDocument.xml";

       // Create FileStream    
       System.IO.FileStream fsWriteXml = 
           new System.IO.FileStream
           (xmlFilename, System.IO.FileMode.Create);

       // Create an XmlTextWriter to write the file.
       System.Xml.XmlTextWriter xmlWriter = 
           new System.Xml.XmlTextWriter
           (fsWriteXml, System.Text.Encoding.Unicode);

       // Use WriteXml to write the document.
       OriginalDataSet.WriteXml(xmlWriter, XmlWriteMode.WriteSchema); 

       // Close the FileStream.
       fsWriteXml.Close();
      
       // Dispose of the original DataSet.
       OriginalDataSet.Dispose();

       // Create a new DataSet.
       DataSet newDataSet = new DataSet("New DataSet");
      
       // Read the XML document back in. 
       // Create new FileStream to read schema with.
       System.IO.FileStream fsReadXml = 
           new System.IO.FileStream
           (xmlFilename, System.IO.FileMode.Open);

       // Create an XmlTextReader to read the file.
       System.Xml.XmlTextReader xmlReader = 
           new System.Xml.XmlTextReader(fsReadXml);

       // Read the XML document into the DataSet.
       newDataSet.ReadXml(xmlReader, XmlReadMode.ReadSchema);

       // Close the XmlTextReader
       xmlReader.Close();

       // Print out values of each table in the DataSet 
       // fusing the unction defined below.
       PrintValues(newDataSet,"New DataSet");
   }

   private void PrintValues(DataSet dataSet, string label)
   {
       Console.WriteLine("\n" + label);
       foreach(DataTable table in dataSet.Tables)
       {
           Console.WriteLine("TableName: " + table.TableName);
           foreach(DataRow row in table.Rows)
           {
               foreach(DataColumn column in table.Columns)
               {
                   Console.Write("\t{0}({1})='{2}'", column.ColumnName, column.DataType.Name, row[column]);
               }
               Console.WriteLine();
           }
       }
   }
开发者ID:.NET开发者,项目名称:System.Data,代码行数:91,代码来源:DataSet.ReadXml

示例4: Main

//引入命名空间
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        DataSet thisDataSet = new DataSet();
        thisDataSet.ReadXml("nwinddata.xml");

        foreach (DataRow custRow in thisDataSet.Tables["Customers"].Rows) {
            Console.WriteLine("Customer ID: " + custRow["CustomerID"] +
                              " Name: " + custRow["CompanyName"]);
        }

        Console.WriteLine("Table created by ReadXml is called {0}",
                           thisDataSet.Tables[0].TableName);
    }
}
开发者ID:C#程序员,项目名称:System.Data,代码行数:20,代码来源:DataSet.ReadXml


注:本文中的System.Data.DataSet.ReadXml方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。