无法 XML 来自对象的数据
Not possible to XML data from object
我有一个非常简单的应用程序,其中 XML 文件被反序列化为对象。当我尝试从对象中读取值时,我得到 null
。
我的 XML 文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<HomePage>http://www.google.dk</HomePage>
<DefaultAudioLevel>100</DefaultAudioLevel>
</Settings>
我的模型是这样的:
[Serializable()]
public class Settings
{
[XmlElement("HomePage")]
public string Homepage { get; set; }
[XmlElement("DefaultAudioLevel")]
public string DefaultAudioLevel { get; set; }
}
和 SettingSerializer.cs
:
public class SettingSerializer
{
private string path;
private string EXE = Assembly.GetExecutingAssembly().GetName().Name;
Settings settings = null;
public SettingSerializer(string xmlPath = null)
{
path = new FileInfo(xmlPath ?? EXE + ".xml").FullName.ToString();
}
public void Deserialize()
{
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
StreamReader reader = new StreamReader(path);
settings = (Settings)serializer.Deserialize(reader);
reader.Close();
}
}
还有我的测试class:
static void Main(string[] args)
{
SettingSerializer serializer = new SettingSerializer();
serializer.Deserialize();
Settings settings = new Settings();
Console.WriteLine(settings.Homepage);
Console.WriteLine(settings.DefaultAudioLevel);
Console.ReadKey();
}
有人能找出错误吗?
将您的 Serializer
更改为:
public class SettingSerializer
{
private string path;
private string EXE = Assembly.GetExecutingAssembly().GetName().Name;
public SettingSerializer(string xmlPath = null)
{
path = new FileInfo(xmlPath ?? EXE + ".xml").FullName.ToString();
}
public Settings Deserialize()
{
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
StreamReader reader = new StreamReader(path);
var settings = (Settings)serializer.Deserialize(reader);
reader.Close();
return settings;
}
}
然后在你的主要方法中你可以这样做:
static void Main(string[] args)
{
SettingSerializer serializer = new SettingSerializer();
Settings settings = serializer.Deserialize();
Console.WriteLine(settings.Homepage);
Console.WriteLine(settings.DefaultAudioLevel);
Console.ReadKey();
}
正如您最初所拥有的那样,反序列化的 settings
与 main 方法中包含的 settings
之间没有任何联系。您只需创建一个新对象并打印出其默认值,在您的情况下为 null
或空字符串。
我有一个非常简单的应用程序,其中 XML 文件被反序列化为对象。当我尝试从对象中读取值时,我得到 null
。
我的 XML 文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
<HomePage>http://www.google.dk</HomePage>
<DefaultAudioLevel>100</DefaultAudioLevel>
</Settings>
我的模型是这样的:
[Serializable()]
public class Settings
{
[XmlElement("HomePage")]
public string Homepage { get; set; }
[XmlElement("DefaultAudioLevel")]
public string DefaultAudioLevel { get; set; }
}
和 SettingSerializer.cs
:
public class SettingSerializer
{
private string path;
private string EXE = Assembly.GetExecutingAssembly().GetName().Name;
Settings settings = null;
public SettingSerializer(string xmlPath = null)
{
path = new FileInfo(xmlPath ?? EXE + ".xml").FullName.ToString();
}
public void Deserialize()
{
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
StreamReader reader = new StreamReader(path);
settings = (Settings)serializer.Deserialize(reader);
reader.Close();
}
}
还有我的测试class:
static void Main(string[] args)
{
SettingSerializer serializer = new SettingSerializer();
serializer.Deserialize();
Settings settings = new Settings();
Console.WriteLine(settings.Homepage);
Console.WriteLine(settings.DefaultAudioLevel);
Console.ReadKey();
}
有人能找出错误吗?
将您的 Serializer
更改为:
public class SettingSerializer
{
private string path;
private string EXE = Assembly.GetExecutingAssembly().GetName().Name;
public SettingSerializer(string xmlPath = null)
{
path = new FileInfo(xmlPath ?? EXE + ".xml").FullName.ToString();
}
public Settings Deserialize()
{
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
StreamReader reader = new StreamReader(path);
var settings = (Settings)serializer.Deserialize(reader);
reader.Close();
return settings;
}
}
然后在你的主要方法中你可以这样做:
static void Main(string[] args)
{
SettingSerializer serializer = new SettingSerializer();
Settings settings = serializer.Deserialize();
Console.WriteLine(settings.Homepage);
Console.WriteLine(settings.DefaultAudioLevel);
Console.ReadKey();
}
正如您最初所拥有的那样,反序列化的 settings
与 main 方法中包含的 settings
之间没有任何联系。您只需创建一个新对象并打印出其默认值,在您的情况下为 null
或空字符串。