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

时间:2018-04-24 19:48:02

标签: c# xml-serialization

我有这堂课:

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

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

1 个答案:

答案 0 :(得分:1)

你快到了。使用:

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