using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DataTable productTable = new DataTable("product"); productTable.Columns.Add("ID",typeof (System.Int32));
productTable.Columns.Add("Name",typeof (System .String ));
productTable.Columns.Add("Category",typeof (System .Int32 )); productTable.PrimaryKey = new DataColumn[]{productTable.Columns["ID"]};
productTable.Columns["ID"].AutoIncrement = true;
productTable.Columns["ID"].AutoIncrementSeed = 1;
productTable.Columns["ID"].ReadOnly = true;
DataRow tempRow; for (int i = 0; i < 10; i++)
{
tempRow = productTable.NewRow(); if (Math.IEEERemainder(i, 2) == 0)
{
tempRow["Name"] = "Caterham Seven de Dion #" + i.ToString();
tempRow["Category"] = 1;
}
else
{
tempRow["Name"] = "Dodge Viper #" + i.ToString();
tempRow["Category"] = 2;
}
productTable.Rows .Add (tempRow );
}
productTable.WriteXml("productTable.xml");
}
}
}
productTable.WriteXml("productTable.xml"); 为什么这里写入不了数据呢???
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DataTable productTable = new DataTable("product"); productTable.Columns.Add("ID",typeof (System.Int32));
productTable.Columns.Add("Name",typeof (System .String ));
productTable.Columns.Add("Category",typeof (System .Int32 )); productTable.PrimaryKey = new DataColumn[]{productTable.Columns["ID"]};
productTable.Columns["ID"].AutoIncrement = true;
productTable.Columns["ID"].AutoIncrementSeed = 1;
productTable.Columns["ID"].ReadOnly = true;
DataRow tempRow; for (int i = 0; i < 10; i++)
{
tempRow = productTable.NewRow(); if (Math.IEEERemainder(i, 2) == 0)
{
tempRow["Name"] = "Caterham Seven de Dion #" + i.ToString();
tempRow["Category"] = 1;
}
else
{
tempRow["Name"] = "Dodge Viper #" + i.ToString();
tempRow["Category"] = 2;
}
productTable.Rows .Add (tempRow );
}
productTable.WriteXml("productTable.xml");
}
}
}
productTable.WriteXml("productTable.xml"); 为什么这里写入不了数据呢???
楼主再试试
在bin\Debug目录下会生成文件,如果不能生成,查看下编译目录有没有问题,看看是不是只读的.<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<product>
<ID>1</ID>
<Name>Caterham Seven de Dion #0</Name>
<Category>1</Category>
</product>
<product>
<ID>2</ID>
<Name>Dodge Viper #1</Name>
<Category>2</Category>
</product>
<product>
<ID>3</ID>
<Name>Caterham Seven de Dion #2</Name>
<Category>1</Category>
</product>
<product>
<ID>4</ID>
<Name>Dodge Viper #3</Name>
<Category>2</Category>
</product>
<product>
<ID>5</ID>
<Name>Caterham Seven de Dion #4</Name>
<Category>1</Category>
</product>
<product>
<ID>6</ID>
<Name>Dodge Viper #5</Name>
<Category>2</Category>
</product>
<product>
<ID>7</ID>
<Name>Caterham Seven de Dion #6</Name>
<Category>1</Category>
</product>
<product>
<ID>8</ID>
<Name>Dodge Viper #7</Name>
<Category>2</Category>
</product>
<product>
<ID>9</ID>
<Name>Caterham Seven de Dion #8</Name>
<Category>1</Category>
</product>
<product>
<ID>10</ID>
<Name>Dodge Viper #9</Name>
<Category>2</Category>
</product>
</DocumentElement>
public static void ConvertDataSetToXMLFile(DataSet xmlDS,string xmlFile)
{
MemoryStream stream = null;
XmlTextWriter writer = null; try
{
stream = new MemoryStream();
writer = new XmlTextWriter(stream, Encoding.Unicode); xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count); UnicodeEncoding utf = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(xmlFile);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(utf.GetString(arr).Trim());
sw.Close();
}
catch( System.Exception ex )
{
throw ex;
}
finally
{
if (writer != null) writer.Close();
}
} }
}