我可以将系统类型序列化/反序列化为XML吗?

时间:2013-07-27 07:22:15

标签: c# wpf xml serialization

我想将模型保存为人们可以加载,保存和导入的单个XML文件。 我可以将Enums和System.Windows.Media.Color等系统类型序列化/反序列化为XML文件吗?

 public enum WeatherType
 {
    Rainy,
    Sunny,
    Cloudy,
    Windy
 }

[Serializable]
[XmlRoot("Profile")]
public class Profile
{
    public string ProfileName { get; set; }
    public System.Windows.Media.Color ProfileColor { get; set; }
    public string City { get; set; }
    public double MinTemp { get; set; }
    public double MaxTemp { get; set; }
    public List<WeatherType> WeatherTypes { get; set; }
}

似乎无法使其发挥作用:/

2 个答案:

答案 0 :(得分:1)

enumColor按定义可序列化(使用SerializableAttribute修饰),因此序列化enumColor属性时不会出现任何问题,看看我的代码示例:

public class Program
{
    public static void Main()
    {
        using (var writer = XmlWriter.Create(Console.OpenStandardOutput()))
        {
            var telAvivProfile = new Profile
                {
                    City = "Tel Aviv",
                    MaxTemp = 40,
                    MinTemp = 5,
                    ProfileColor = Color.FromRgb(4, 4, 4),
                    WeatherTypes = new List<WeatherType>
                        {
                            WeatherType.Sunny,
                            WeatherType.Rainy
                        }
                };

            var serializer = new XmlSerializer(telAvivProfile.GetType());
            serializer.Serialize(writer, telAvivProfile);

            Console.WriteLine(writer.ToString());
        }
        Console.ReadLine();
    }
}

public enum WeatherType
{
    Rainy,
    Sunny,
    Cloudy,
    Windy
}

[Serializable]
[XmlRoot("Profile")]
public class Profile
{
    public string ProfileName { get; set; }
    public System.Windows.Media.Color ProfileColor { get; set; }
    public string City { get; set; }
    public double MinTemp { get; set; }
    public double MaxTemp { get; set; }
    public List<WeatherType> WeatherTypes { get; set; }
}

将生成以下XML:

<?xml version="1.0" encoding="utf-8"?>
<Profile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ProfileColor="">
    <A>255</A>
    <R>4</R>
    <G>4</G>
    <B>4</B>
    <ScA>1</ScA>
    <ScR>0.001214108</ScR>
    <ScG>0.001214108</ScG>
    <ScB>0.001214108</ScB>
    </ProfileColor>
    <City>Tel Aviv</City>
    <MinTemp>5</MinTemp>
    <MaxTemp>45</MaxTemp>
    <WeatherTypes>
      <WeatherType>Sunny</WeatherType>
      <WeatherType>Rainy</WeatherType>
    </WeatherTypes>
</Profile>

此外,如果您想要控制enum字段的序列化方式,那么您可以使用XmlEnum属性修饰每个字段,例如:

public enum WeatherType
{
    [XmlEnum(Name="Hot")]
    Rainy,
    Sunny,
    Cloudy,
    Windy
}

以下是如何从文件中反序列化/加载XML:

    Profile loadedProfile = null;
    string path = "telAvivProfile.xml";

    XmlSerializer serializer = new XmlSerializer(typeof (Profile));
    StreamReader reader = new StreamReader(path);
    loadedProfile = (Profile) serializer.Deserialize(reader);

    reader.Close();

答案 1 :(得分:1)

不确定问题是什么。你能提供更多信息。

以下是示例代码

static void Main(string[] args)
{
    Profile p = new Profile();
    p.ProfileColor = System.Windows.Media.Color.FromArgb(1, 1, 1, 0);
    p.WeatherTypes = new List<WeatherType>
        {
            WeatherType.Cloudy,
            WeatherType.Windy
        };
    var serializer = new XmlSerializer(typeof(Profile));
    var sb = new StringBuilder();
    TextWriter writer = new StringWriter(sb);
    serializer.Serialize(writer, p);
    Console.WriteLine(sb.ToString());
}

这是XML

<?xml version="1.0" encoding="utf-16"?>
<Profile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ProfileColor>
    <A>1</A>
    <R>1</R>
    <G>1</G>
    <B>0</B>
    <ScA>0.003921569</ScA>
    <ScR>0.000303527</ScR>
    <ScG>0.000303527</ScG>
    <ScB>0</ScB>
  </ProfileColor>
  <MinTemp>0</MinTemp>
  <MaxTemp>0</MaxTemp>
  <WeatherTypes>
    <WeatherType>Cloudy</WeatherType>
    <WeatherType>Windy</WeatherType>
  </WeatherTypes>
</Profile>