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

C# 我可以将系统类型序列化/反序列化为XML吗?,c#,wpf,xml,serialization,C#,Wpf,Xml,Serialization,我想将我的模型保存为人们可以加载、保存和导入的单个XML文件。 我是否可以将系统类型(如enum和System.Windows.Media.Color)序列化/反序列化为XML文件 public enum WeatherType { Rainy, Sunny, Cloudy, Windy } [Serializable] [XmlRoot("Profile")] public class Profile { public string Profil

我想将我的模型保存为人们可以加载、保存和导入的单个XML文件。 我是否可以将系统类型(如enum和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; }
}
公共枚举类型
{
阴雨的
晴朗的
多云的
多风的
}
[可序列化]
[XmlRoot(“配置文件”)]
公共班级简介
{
公共字符串ProfileName{get;set;}
public System.Windows.Media.Color ProfileColor{get;set;}
公共字符串City{get;set;}
公共双MinTemp{get;set;}
公共双MaxTemp{get;set;}
公共列表天气类型{get;set;}
}

似乎无法正常工作:/

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:

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

不知道问题出在哪里。你能提供更多的信息吗

下面是一个示例代码

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());
}
static void Main(字符串[]args)
{
外形p=新外形();
p、 ProfileColor=System.Windows.Media.Color.FromArgb(1,1,1,0);
p、 WeatherTypes=新列表
{
天气类型,多云,
天气型,多风
};
var serializer=新的XmlSerializer(typeof(Profile));
var sb=新的StringBuilder();
TextWriter=新的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>

1.
1.
1.
0
0.003921569
0.000303527
0.000303527
0
0
0
多云的
多风的

枚举可根据定义序列化<代码>颜色似乎也可以序列化。有什么问题?什么不起作用?你们能把代码贴在你们面临的问题上吗?请看我的答案。
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 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>