S
SebGM2018
Guest
ood Day, I Need to Deserialize Different XML Files In My Windows Forms Application, It Should Work Like The Following.So I have Tried So Much Alternatives, But This Is The Code That Is The Closest To My Goal.
`private OpenFileDialog openFileDialog1 = new OpenFileDialog();
private void ImportIcon_Click(object sender, EventArgs e)
{
IfSound();
openFileDialog1.Filter = "Analytica Files (*.analy) |*.analy";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var Path = openFileDialog1.FileName;
foreach (string file in openFileDialog1.FileNames)
{
Settings.Default.FileList.Add(file);
Settings.Default.Save();
//Settings.Default.Upgrade();
}
//XmlSerializer XS = new
//XmlSerializer(typeof(List<Information>));
//StreamReader Reader = new StreamReader(Path);
//Information i;
//var input = XS.Deserialize(Reader);
//Settings.Default.Counter++;
//Settings.Default.Save();
}
else
{
MessageBox.Show("Exception Found In File");
}
}
foreach (string TheFile in Settings.Default.FileList)
{
Reminders.TaskUC Task = new Reminders.TaskUC();
Point TP = new Point();
Task.Name = "Task" + Settings.Default.Counter.ToString();
TP.Y = 1;
int Add = 300;
int Result = Start;
int Distance = 100;
Control Last = Controls[Controls.Count - 1];
TP.X = Last.Location.X + Distance;
if (Settings.Default.FileList.Count == 1)
{
TP.X = 300;
}
Size PanelWidth = new Size();
PanelWidth.Width = ReminderPanel.Width + 300;
PanelWidth.Height = ReminderPanel.Height;
Task.Location = TP;
this.Controls.Add(Task);
ReminderPanel.Size = PanelWidth;
ReminderPanel.Controls.Add(Task);
XmlSerializer XS = new XmlSerializer(typeof(List<Information>));
FileStream Read = new FileStream(TheFile, FileMode.Open,
FileAccess.Read, FileShare.Read);
Information i = new Information();
XS.Deserialize(Read);
Task.TitleBox.Text = i.Title1;
}
This is the class code:
public class Information
{
private string Title;
private string Description;
private DateTime Date;
private DateTime Hour;
private bool Check;
public string Title1
{
get { return Title; }
set { Title = value; }
}
public string Description2
{
get { return Description; }
set { Description = value; }
}
public DateTime Date3
{
get { return Date; }
set { Date = value; }
}
public DateTime Hour4
{
get { return Hour; }
set { Hour = value; }
}
public bool Check5
{
get { return Check; }
set { Check = value;}
}
public class SaveXML
{
public static void SaveData(object obj, string filename)
{
XmlSerializer SR = new XmlSerializer(obj.GetType());
TextWriter Writer = new StreamWriter(filename);
SR.Serialize(Writer, obj);
Writer.Close();
} `
As you can see, FileList is a List<string> saved in my user settings, to get and save the openfiledialog file paths in it, then to deserialize it, but it has a problem that doesn't really save the strings even when the Save(); method is called.
Continue reading...
`private OpenFileDialog openFileDialog1 = new OpenFileDialog();
private void ImportIcon_Click(object sender, EventArgs e)
{
IfSound();
openFileDialog1.Filter = "Analytica Files (*.analy) |*.analy";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var Path = openFileDialog1.FileName;
foreach (string file in openFileDialog1.FileNames)
{
Settings.Default.FileList.Add(file);
Settings.Default.Save();
//Settings.Default.Upgrade();
}
//XmlSerializer XS = new
//XmlSerializer(typeof(List<Information>));
//StreamReader Reader = new StreamReader(Path);
//Information i;
//var input = XS.Deserialize(Reader);
//Settings.Default.Counter++;
//Settings.Default.Save();
}
else
{
MessageBox.Show("Exception Found In File");
}
}
foreach (string TheFile in Settings.Default.FileList)
{
Reminders.TaskUC Task = new Reminders.TaskUC();
Point TP = new Point();
Task.Name = "Task" + Settings.Default.Counter.ToString();
TP.Y = 1;
int Add = 300;
int Result = Start;
int Distance = 100;
Control Last = Controls[Controls.Count - 1];
TP.X = Last.Location.X + Distance;
if (Settings.Default.FileList.Count == 1)
{
TP.X = 300;
}
Size PanelWidth = new Size();
PanelWidth.Width = ReminderPanel.Width + 300;
PanelWidth.Height = ReminderPanel.Height;
Task.Location = TP;
this.Controls.Add(Task);
ReminderPanel.Size = PanelWidth;
ReminderPanel.Controls.Add(Task);
XmlSerializer XS = new XmlSerializer(typeof(List<Information>));
FileStream Read = new FileStream(TheFile, FileMode.Open,
FileAccess.Read, FileShare.Read);
Information i = new Information();
XS.Deserialize(Read);
Task.TitleBox.Text = i.Title1;
}
This is the class code:
public class Information
{
private string Title;
private string Description;
private DateTime Date;
private DateTime Hour;
private bool Check;
public string Title1
{
get { return Title; }
set { Title = value; }
}
public string Description2
{
get { return Description; }
set { Description = value; }
}
public DateTime Date3
{
get { return Date; }
set { Date = value; }
}
public DateTime Hour4
{
get { return Hour; }
set { Hour = value; }
}
public bool Check5
{
get { return Check; }
set { Check = value;}
}
public class SaveXML
{
public static void SaveData(object obj, string filename)
{
XmlSerializer SR = new XmlSerializer(obj.GetType());
TextWriter Writer = new StreamWriter(filename);
SR.Serialize(Writer, obj);
Writer.Close();
} `
As you can see, FileList is a List<string> saved in my user settings, to get and save the openfiledialog file paths in it, then to deserialize it, but it has a problem that doesn't really save the strings even when the Save(); method is called.
Continue reading...