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"); 为什么这里写入不了数据呢???

解决方案 »

  1.   

    看了下   程序没得问题啊 using System.Linq; 是啥东东哦  你在debug下面看下应该可以的 啊  
      

  2.   

    楼主代码没问题,我运行通过,生成的XML文件在程序exe同一级目录下
    楼主再试试
      

  3.   

    直接把lz的代码copy下来跑了一下,没问题啊。除了Linq的引用没用外其它都没问题。
      

  4.   

    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>
      

  5.   

    //将DataSet转换为xml文件
            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();
                }
            }    }
    }