Csharp/CSharp Tutorial/XML/Xml DataSet — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:17, 26 мая 2010

Create Data with DataSet and DataRow and save to xml

using System;
using System.Data;
public enum DiscountType {
  Percentage,
  Fixed
}
public class CreateData {
  public static void Main(string [] args) {
    DataSet dataSet = new DataSet();
    dataSet.ReadXmlSchema("Coupons.xsd");
    
    DataTable couponsTable = dataSet.Tables["coupons"];
    
    DataRow couponRow = couponsTable.NewRow();
    couponRow["mycode"] = "763FF";
    couponRow["amount"] = 0.5;
    couponRow["mytype"] = DiscountType.Fixed;
    couponRow["my_date"] = new DateTime(2002,12,31);
    couponsTable.Rows.Add(couponRow);
    
    dataSet.WriteXml("Coupons.xml");
  }
}

Fill DataSet with the data from XML

using System;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Data.rumon;
using System.Data;
class MainClass
{
  static void Main(string[] args)
  {
    DataSet ds = new DataSet();
    
    ds.ReadXml(@"c:\Sample.xml ");
  }
}

Load XML to DataSet

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Xml;
public class LoadXMLDataSet : System.Windows.Forms.Form
{
  private DataGrid dataGrid1= new DataGrid();
  public LoadXMLDataSet()
  {
    this.dataGrid1.DataMember = "";
    this.dataGrid1.Location = new System.Drawing.Point(8, 16);
    this.dataGrid1.Size = new System.Drawing.Size(264, 232);
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {this.dataGrid1});
           
    XmlDataDocument xmlDatadoc = new XmlDataDocument();
    xmlDatadoc.DataSet.ReadXml("C:\\books.xml");
    DataSet ds = new DataSet("Books DataSet");
    ds = xmlDatadoc.DataSet;
    dataGrid1.DataSource = ds.DefaultViewManager; 
  }
  static void Main() 
  {
    Application.Run(new LoadXMLDataSet());
  }
}

Output DataSet to Xml

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
  class Program
  {
    static void Main(string[] args)
    {
      DataSet carsInventoryDS = new DataSet("Car");
      carsInventoryDS.ExtendedProperties["TimeStamp"] = DateTime.Now;
      carsInventoryDS.ExtendedProperties["DataSetID"] = Guid.NewGuid();
      carsInventoryDS.ExtendedProperties["Company"] = "Training";
      DataColumn carIDColumn = new DataColumn("CarID", typeof(int));
      carIDColumn.Caption = "Car ID";
      carIDColumn.ReadOnly = true;
      carIDColumn.AllowDBNull = false;
      carIDColumn.Unique = true;
      carIDColumn.AutoIncrement = true;
      carIDColumn.AutoIncrementSeed = 0;
      carIDColumn.AutoIncrementStep = 1;
      DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
      DataColumn carColorColumn = new DataColumn("Color", typeof(string));
      DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
      carPetNameColumn.Caption = "Pet Name";
      DataTable inventoryTable = new DataTable("Inventory");
      inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn, carMakeColumn, carColorColumn, carPetNameColumn });
      DataRow carRow = inventoryTable.NewRow();
      carRow["Make"] = "BMW";
      carRow["Color"] = "Black";
      carRow["PetName"] = "Hamlet";
      inventoryTable.Rows.Add(carRow);
      carRow = inventoryTable.NewRow();
      carRow[1] = "A";
      carRow[2] = "B";
      carRow[3] = "C";
      inventoryTable.Rows.Add(carRow);
      inventoryTable.PrimaryKey = new DataColumn[] { inventoryTable.Columns[0] };
      carsInventoryDS.Tables.Add(inventoryTable);
      
      // Save this DataSet as XML.
      carsInventoryDS.WriteXml("carsDataSet.xml");
      carsInventoryDS.WriteXmlSchema("carsDataSet.xsd");
      // Clear out DataSet.
      carsInventoryDS.Clear();
      // Load DataSet from XML file.
      carsInventoryDS.ReadXml("carsDataSet.xml");
      
    }
  }

Reading XML with DataSet

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);
    }
  }

Save data in a DataSet to xml document

using System;
using System.IO;
using System.Xml;
using System.Data;
class MainClass
{
  public static void Main()
  {
    DataSet ds = new DataSet("DS");
    ds.Namespace = "StdNamespace";
    DataTable stdTable = new DataTable("Student");
    DataColumn col1 = new DataColumn("Name");
    DataColumn col2 = new DataColumn("Address");
    stdTable.Columns.Add(col1);
    stdTable.Columns.Add(col2);
    ds.Tables.Add(stdTable);
    DataRow newRow;newRow = stdTable.NewRow();
    newRow["Name"]= "M C";
    newRow["Address"]= "address 1";
    stdTable.Rows.Add(newRow);
    newRow = stdTable.NewRow();
    newRow["Name"]= "M G";
    newRow["Address"]= "address1";
    stdTable.Rows.Add(newRow);
    ds.AcceptChanges();
    StreamWriter myStreamWriter = new StreamWriter(@"c:\stdData.xml");
    ds.WriteXml(myStreamWriter);
    myStreamWriter.Close();
  }
}