美好的一天,我需要在Windows窗体应用程序中反序列化不同的XML文件,它的工作方式如下所示。 所以我尝试了很多选择,但这是离我的目标最近的代码。
`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();
} `
此行运行时,
XS.Deserialize(Read);
该应用程序停止,并显示以下错误:
System.InvalidOperationException:'XML文档(2,2)中的错误。 内部异常。 InvalidOperationException:不期望。 (有时显示(0,0)。而不是(2,2)。)
修改
这是我的XML文件代码,奇怪的是,另一个XML文件是空的,就像它们里面什么都没有一样,里面是这样的数据:
<?xml version="1.0" encoding="utf-8"?>
<Information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title1>12</Title1>
<Description2>Description</Description2>
<Date3>2018-02-27T22:05:39</Date3>
<Hour4>2018-02-27T22:05:39</Hour4>
</Information>
答案 0 :(得分:1)
您的代码可以与以下项目配合使用
XS = new XmlSerializer(typeof(Information));
在更改XML以使其匹配之前,您似乎已经更改了要反序列化的类型:
new XmlSerializer(typeof(List<Information>));
您需要将XML文件更改为此文件,并且该文件应该可以工作:
<?xml version="1.0"?>
<ArrayOfInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Information>
<Title1>12</Title1>
<Description2>Description</Description2>
<Date3>2018-02-27T22:05:39</Date3>
<Hour4>2018-02-27T22:05:39</Hour4>
</Information>
</ArrayOfInformation>