首页 > 解决方案 > 以不同的元素名称序列化一个类

问题描述

我有这堂课:

[XmlRoot(ElementName ="Lesson")]
public class LessonOld
{
    public LessonOld()
    {
        Students = new List<string>();
    }
    public string Name { get; set; }
    public DateTime FirstLessonDate { get; set; }
    public int DurationInMinutes { get; set; }
    public List<string> Students { get; set; }
}

我正在使用此代码对其进行序列化:

TextWriter writer = new StreamWriter(Path.Combine(UserSettings, "Lessons-temp.xml"));
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<LessonOld>));
xmlSerializer.Serialize(writer, tempList);
writer.Close();

(注意那是一个List<LessonOld>

这是我生成的 XML:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfLessonOld xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LessonOld>
    <FirstLessonDate>0001-01-01T00:00:00</FirstLessonDate>
    <DurationInMinutes>0</DurationInMinutes>
    <Students />
  </LessonOld>
</ArrayOfLessonOld>

我想将其更改为序列化为<ArrayOfLessonXML<Lesson>元素。这可能吗?(如您所见,我已经尝试过使用[XmlRoot(ElementName ="Lesson")]

标签: c#xml-serialization

解决方案


您快到了。利用:

[XmlType(TypeName = "Lesson")]

代替

[XmlRoot(ElementName = "Lesson")]

当然你可以很容易地测试它;您的代码进行了上述更改

[XmlType(TypeName = "Lesson")]
public class LessonOld
{
    public LessonOld()
    {
        Students = new List<string>();
    }
    public string Name { get; set; }
    public DateTime FirstLessonDate { get; set; }
    public int DurationInMinutes { get; set; }
    public List<string> Students { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        TextWriter writer = new StreamWriter(Path.Combine(@"C:\Users\Francesco\Desktop\nanovg", "Lessons-temp.xml"));
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<LessonOld>));
        xmlSerializer.Serialize(writer, new List<LessonOld> { new LessonOld() { Name = "name", DurationInMinutes = 0 } });
        writer.Close();
    }
}

产生这个

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfLesson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Lesson>
    <Name>name</Name>
    <FirstLessonDate>0001-01-01T00:00:00</FirstLessonDate>
    <DurationInMinutes>0</DurationInMinutes>
    <Students />
  </Lesson>
</ArrayOfLesson>

正如我所见,XmlRoot当您想要序列化单个对象时可以正常工作。考虑以下代码,源自您的代码:

[XmlRoot(ElementName = "Lesson")]
public class LessonOld
{
    public LessonOld()
    {
        Students = new List<string>();
    }
    public string Name { get; set; }
    public DateTime FirstLessonDate { get; set; }
    public int DurationInMinutes { get; set; }
    public List<string> Students { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        TextWriter writer = new StreamWriter(Path.Combine(@"C:\Users\Francesco\Desktop\nanovg", "Lessons-temp.xml"));
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LessonOld));
        xmlSerializer.Serialize(writer, new LessonOld() { Name = "name", DurationInMinutes = 0  });
        writer.Close();
    }
}

它会输出

<?xml version="1.0" encoding="utf-8"?>
<Lesson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>name</Name>
  <FirstLessonDate>0001-01-01T00:00:00</FirstLessonDate>
  <DurationInMinutes>0</DurationInMinutes>
  <Students />
</Lesson>

推荐阅读