【问题标题】:Can I serialize/deserialize System Types to XML?我可以将系统类型序列化/反序列化为 XML 吗?
【发布时间】:2013-07-27 07:22:15
【问题描述】:

我想将我的模型保存为人们可以加载、保存和导入的单个 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; }
}

似乎无法让它工作:/

【问题讨论】:

  • 枚举根据定义是可序列化的。 Color 似乎也是可序列化的。有什么问题?
  • 什么不起作用?您可以在遇到问题的地方发布代码吗?
  • 请看我的回答。

标签: c# wpf xml serialization


【解决方案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>

【讨论】:

    【解决方案2】:

    enum 和 Color 根据定义是可序列化的(用SerializableAttribute 装饰)所以序列化enum 或Color 属性应该没有任何问题,看看我的代码示例:

    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();
    

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多