c# - 以不同的元素名称序列化一个类
问题描述
我有这堂课:
[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
XML<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>
推荐阅读
- javascript - 使用javascript将带有类名的span添加到字符串的某些字符
- c++ - val1, val2, val3, val4 的值是如何计算的?
- ios - 如何从父 ViewController 继承 @IBOutlets?
- julia - 如何在 Julia 中将两个地块相邻放置?
- kubernetes - K8s CRD 错误:不支持的媒体类型 415
- android - Viewmodel 在第一次之后不会更改 Livedata
- c# - 来自字符串的 C# 条件,包括运算符
- node.js - 如何保持帐户会话登录?
- javascript - 如何从反应导航更改底部材质选项卡导航器的高度
- c++ - 用户定义类型的隐式类型转换