PDA

View Full Version : مبتدی: بازیابی مقادیر از فایل xml



CodeforLife
شنبه 21 فروردین 1395, 12:07 عصر
سلام دوستان گرامی.
من بالاخره موفق شدم یه سری داده ها رو در فایل xml ذخیره کنم !
البته به کمک شما بزرگواران
من دوباره می خوام مقادیر رو بازیابی کنم (deserialize)
کد رو نوشتم exception دارم و با اینکه زیاد تو نت سرچ کردم نمی دونم باید چه کار کنم این خطا از بین بره ...
2 سوال دارم یکی اینکه این خطا رو چه طور برطرف کنم ؟
دوم اینکه اگر من در برنامه در کلاسهام مقدار رنگ color داشته باشم ؛ باید چه کار کنم در xml نمایش داده شود ..؟

notebook overview = new notebook();
XmlSerializer writer = new XmlSerializer(overview.GetType());
var path = Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments) + "//SerializationOverview.xml";
System.IO.FileStream file = System.IO.File.Create(path);
notebook n= (notebook)writer.Deserialize(file);//exception
file.Close();



There is an error in XML document (0, 0).این متن exception

Mahmoud.Afrad
شنبه 21 فروردین 1395, 13:02 عصر
اگر اصرار دارید به صورت xml ذخیره بشه یا باید یک پراپرتی string معادل کد هگز رنگ و یا یک پراپرتی int معادل Argb رنگ به کلاس اضافه کنید تا این پراپرتی ذخیره بشه.


یا اینکه در قالب باینری ذخیره و بازیابی کنید.
برای این کار بایست کلاسهایی که نیاز به ذخیره نمونه های اون دارید را به همراه کلاسهای والد با ویژگی Serializable مشخص کنید.
[Serializable]
public class Notebook : Book
{
[Serializable]
public class Book
{
به صورت زیر میتونید سریالایز کنید
Notebook overview = new Notebook();

var path = Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments) + "//SerializationOverview";
System.IO.FileStream file = System.IO.File.Create(path);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(file , overview);
file.Close();
دیسریالایز هم به صورت زیر. (فایل در صورت وجود باید open بشه)
var path = Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments) + "//SerializationOverview";
System.IO.FileStream file = System.IO.File.Open(path , FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
Notebook n = (Notebook) formatter.Deserialize(file);
file.Close();

CodeforLife
یک شنبه 22 فروردین 1395, 03:20 صبح
سلام نوشتم به درستی سریالایز میشه ...
ولی در زمان دیسریالایز یه exception دارم که میگه
متن exception : There is an error in XML document (2, 2).
inner exception : <Circle xmlns=''> was not expected.
circle اسم کلاس منه البته چند تا کلاس دیگه هم دارم که برای همه همینطوره !!!!
کاملا گیج شدم تو هر سایتی سرچ کردم چیز مناسبی پیدا نمی کنم که بتونه کمکم کنه !!!!
خواهش میکنم کمکم کنید...

CodeforLife
یک شنبه 22 فروردین 1395, 03:23 صبح
using System.Xml.Serialization;


namespace Paint
{
[Serializable ]
public class Circle : FillableShape
{
static int ShapeNumber = 0;
public Circle()
{
++ShapeNumber;
this.name = "Circle" + ShapeNumber.ToString();
width = 80;
height = 80;
x = 300;
y = 370;

}
[XmlElement("width")]

public float width { get; set; }
[XmlElement("height")]

public float height { get; set; }
[XmlElement("x")]

public float x { get; set; }
[XmlElement("y")]

public float y { get; set; }
//
public override void Draw(Graphics g)
{
switch (fillstyle)
{
case FillStyleEnum.None:
Brush brush = new SolidBrush(fillcolor);
g.FillEllipse(brush, x, y, width, height);

break;
case FillStyleEnum.Hatch:
HatchBrush hBrush = new HatchBrush(HatchStyle.DiagonalCross, Color.Black, fillcolor);
g.FillEllipse(hBrush, x, y, width, height);
break;
case FillStyleEnum.Gradient:
LinearGradientBrush gBrush = new LinearGradientBrush(new Point(0, 0), new Point(200, 10), fillcolor, Color.FromArgb(255, 0, 255, 0));
g.FillEllipse(gBrush, x, y, width, height);
break;
}
g.DrawEllipse(new Pen(strockcolor, strockwidth), x, y, width, height);
}

//public override string Save()
//{
// return name + ":" + width + ":" + height + ":" + x + ":" + y;
//}

}
}



کلاس circle