我已經反序列化了一個xml文件,其中包含程序列表,天數,時間以及對它們的真或假值。該文件看起來類似於下面。獲取反序列化值
<AlarmSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ProgramSettings>
<ProgramSetting>
<ProgramPathText>Alarm.exe</ProgramPathText>
<ProgramPathValue>D:\Documents\Work\Visual Studio\WindowsFormsApplication1\bin\Debug\Alarm.exe</ProgramPathValue>
<Monday>
<Time>11:08</Time>
<Enabled>true</Enabled>
</Monday>
<Tuesday>
<Time>17:08</Time>
<Enabled>true</Enabled>
</Tuesday>
</ProgramSetting>
</ProgramSettings>
</AlarmSettings>
我試圖訪問值,但我一直在程序設置結束陷在那裏我看不到,這將是非常有用的任何方法。我需要返回programpathtext值,programpathvalue值等。
public void load()
{
AlarmSettings alarmSettings;
alarmSettings = AlarmSettings.Load(@"C:\Users\jason\Desktop\Booya.txt");
alarmSettings.ProgramSettings.
}
任何幫助,將不勝感激。由於
AlarmSettings類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
namespace WindowsAlarm
{
public class AlarmSettings
{
public List<ProgramSetting> ProgramSettings = new List<ProgramSetting>();
public void Save(string filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(AlarmSettings));
TextWriter writer = new StreamWriter(filename);
serializer.Serialize(writer, this);
writer.Close();
}
public static AlarmSettings Load(string filename)
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(AlarmSettings));
using (StreamReader reader = new StreamReader(filename))
{
AlarmSettings loadedSettings = (AlarmSettings)serializer.Deserialize(reader);
reader.Close();
return loadedSettings;
}
}
catch(Exception e)
{
throw e;
//return new AlarmSettings();
}
}
}
}
什麼是您的代碼中的AlarmSettings? –
請發佈您的AlarmSettings的定義以及 – vengets
您是如何反序列化的?顯示反序列化代碼會很有幫助。 –