dataset对XML的操作。writexml() and readxml。dataset.AcceptChanges()。dataset.DIspose()。释放资源

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();
        }
    }
}
时间: 2025-01-02 04:20:08

dataset对XML的操作。writexml() and readxml。dataset.AcceptChanges()。dataset.DIspose()。释放资源的相关文章

DataSet与Xml文件的互相转换

DataSet转换为xml文件   //将DataSet转换为xml文件        private static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)        {            MemoryStream stream = null;            XmlTextWriter writer = null;                        try            {    

[No0000DE]C# XmlHelper XML类型操作 类封装

using System; using System.Data; using System.IO; using System.Text; using System.Threading; using System.Xml; using System.Xml.Serialization; using Shared; namespace Helpers { public static class XmlHelper { #region Fields and Properties public enum

c#解析XML到DATASET及dataset转为xml文件函数

//将xml对象内容字符串转换为DataSet public static DataSet ConvertXMLToDataSet(string xmlData) { StringReader stream = null; XmlTextReader reader = null; try { DataSet xmlDS = new DataSet(); stream = new StringReader(xmlData); //从stream装载到XmlTextReader reader = n

DataTable / DataSet 与 xml 的相互转换

之前做DataTable和DataSet转xml一直使用XmlSerializer 序列化完成.今天发现新方法,哇咔咔方便了很多.还不用担心Name为空时报错 1 static void Main(string[] args) 2 { 3 DataTable dt = new DataTable(); 4 dt.TableName = "dtABC"; 5 dt.Columns.Add("CloumnA"); 6 dt.Columns.Add("Cloum

Python实现XML的操作

本文从以下两个方面, 用Python实现XML的操作: 一. minidom写入XML示例1 二. minidom写入XML示例2 三. ElementTree写入/修改示例 四. ElementTree读取/修改/写入示例 一. minidom写入XML示例1 1.引入包 import xml.dom.minidom 2.write XML方法 writexml(writer, indent, addindent, newl, encoding) writer是文件对象 indent是每个ta

xml文件操作

一.xml文件操作1.了解xml文件的SAX解析方式 1.1SAX解析XML  //第一步:解析xml的对象  //工厂模式   SAXParser parser = SAXParserFactory       .newInstance()       .newSAXParser();  //第二步:获得xml文件   InputStream is = getResources().openRawResource(R.raw.shu);  //第三步: 处理器   DefaultHandler

XML常用操作

C#操作XML非常简单 一是写入: 生明一个xelment 再在它的父节点Add就可以,也可以用生明的节点.SetAttribute("节点名称","节点对应的值"); 二是读取: 一样也是先生明再得到其子节点再                    if (xe.Name == "Contrast")                                            if (xe.GetAttribute("IsS

Linq To XML 简单操作

加载xml文件和保存xml文件 XDocument doc = XDocument.Load(Server.MapPath("\\xmlfile\\Book.xml")); doc.Save(Server.MapPath("\\xmlfile\\BookBackup.xml")); 创建元素 XElement xe = new XElement("book",new XElement("bookname","asp.

python解析xml文件操作的例子

python解析xml文件操作实例,操作XML文件的常见技巧. xml文件内容: <?xml version="1.0" ?> <!--Simple xml document__chapter 8--> <book> <title> sample xml thing </title> <author> <name> <first> ma </first> <last>