在不同的元素名称下序列化 class

Serializing a class under a different element name

我有这个class:

[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>

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

你快到了。使用:

[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>