gpt4 book ai didi

c# - 在不同的元素名称下序列化一个类

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:28 25 4
gpt4 key购买 nike

我有这门课:

[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")] )

最佳答案

你快到了。使用:

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

关于c# - 在不同的元素名称下序列化一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50009719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com