PDA

View Full Version : مشکل با ساخت xml



NIMA_1981
پنج شنبه 03 شهریور 1390, 00:24 صبح
سلام دوستان
من یک مثال ساده از روی یک کتاب برای ساخت xml نوشتم برنامه اجرا میشه اما هیچ فابلی ساخته نمیشه میشه راهنمایی کنید

این کد کلاس برنامه هست

using System.IO;
using System.Xml.Serialization;


namespace naeimxml
{
public class SerializableData
{
//Save-serialize the object to dick ...
public void Save(String filename) {
//make a temporary filename...
String tempFilename;

tempFilename =filename +".tmp";
//does the file exist?
FileInfo tempFileInfo= new FileInfo (tempFilename);
if (tempFileInfo.Exists)
tempFileInfo.Delete();
//open the file...
FileStream stream=new FileStream (tempFilename ,FileMode.Create,FileAccess.Write );
// save the object
Save(stream);
//close the file...
stream .Close();
//remove the existing data file and rename the temp file...
tempFileInfo.CopyTo (filename,true);
tempFileInfo.Delete();

}
public void Save(Stream stream)
{
//create a serializer...
XmlSerializer serializer = new XmlSerializer(this.GetType());
//save the file...
serializer.Serialize(stream, this);
}
}
}

navidkhalilian
پنج شنبه 03 شهریور 1390, 01:10 صبح
سلام دوست عزیز اصلاح شده کدی که گذاشتید:


public class SerializableData
{
//Save-serialize the object to dick ...
public void Save(String filename)
{
//make a temporary filename...
String tempFilename;

tempFilename = filename + ".tmp";
//does the file exist?
FileInfo tempFileInfo = new FileInfo(tempFilename);
if (tempFileInfo.Exists)
tempFileInfo.Delete();


//open the file...
FileStream stream = new FileStream(tempFilename, FileMode.Create, FileAccess.Write);
// save the object
Save(stream);
//close the file...
stream.Close();



}
public void Save(Stream stream)
{
//create a serializer...
XmlSerializer serializer = new XmlSerializer(this.GetType());
//save the file...
serializer.Serialize(stream, this);
}



}

private void button1_Click(object sender, EventArgs e)
{
SerializableData sd = new SerializableData();
sd.Save("TypeYourTextHere");
}


موفق باشید.

NIMA_1981
پنج شنبه 03 شهریور 1390, 16:48 عصر
مرسی دوست عزیز یک سوال دیگه اگه بخوام رکورد ها پشت سر هم توی فابل نوشته بشه باید چیکار کنم منطورم اینه که چند رکورد با هم تو فایل باشه الان هر کاری میکنم یک رکورد توی فایل نوشته میشه