方法一
// < Records count = "5" > //< Record > // < Contact_ID > 5 - 55W - 55155 </ Contact_ID > // < Identification_Num > 11111 </ Identification_Num > // </ Record > //DataSet ds = new DataSet("Records"); //DataColumn Cw = new DataColumn("Contact_ID", typeof(string)); //DataColumn Cwnum = new DataColumn("Identification_Num", typeof(string)); //DataTable dt = new DataTable(); ; //for (int i = 0; i <= 20; i++) //{ // dt = ds.Tables.Add("Record"); // dt.Columns.Add(Cw); // dt.Columns.Add(Cwnum); // DataRow dr = dt.NewRow(); // var a = new Random().Next(100200, 99999999); // var b = new Random().Next(200080, 888999999); // dr["Contact_ID"] = a + b; // var c = new Random().Next(100200, 99999999); // var d = new Random().Next(200080, 888999999); // dr["Identification_Num"] = c + d; // dt.Rows.Add(dr); //} //dt.WriteXml("aaaaaaaaaaa.xml"); //Console.WriteLine("ok"); //Console.Read();
方法二
string filename = "aaaaaaaa.xml";//创建一个表示所要生成的XML文件路径和文件字称的字符串 // 创建一个写入XML数据的文件流 System.IO.FileStream myFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Create); // 使用文件流对象创建一个XmlTextWriter对象 System.Xml.XmlTextWriter myXmlWriter = new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);//如果是UTF-8就得改成这个 // 指定文件输出的格式 myXmlWriter.Formatting = System.Xml.Formatting.Indented; try { for (int i = 0; i <= 1000; i++) { var a = Guid.NewGuid().ToString().Substring(0,10); var b = Guid.NewGuid().ToString().Substring(0, 10); // 使用WriteXMLbyXmlWriter方法把数据写入XmlTextWriter对象中 myXmlWriter.WriteStartElement("Record"); myXmlWriter.WriteElementString("Contact_ID", a); myXmlWriter.WriteElementString("Identification_Num", b); myXmlWriter.WriteEndElement(); } // 通过Close方法的调用,XmlTextWriter对象的数据最终写入XML文件 myXmlWriter.Close(); } catch { }