[英]C#: serialize and deserialize XML with different class name
I am getting XML from sql server and i am able to deserialize it with proper Human class. 我从sql server获取XML,并且可以使用适当的Human类反序列化它。
public class Human
{
public string name {get;set;}
}
after giving value for name property i want to serialize it with different root name, because i want to deserialize it again with new class name 在为name属性提供值之后,我想使用不同的根名称对其进行序列化,因为我想使用新的类名再次对其进行反序列化
public class Boy
{
public string name {get;set;}
}
please give a solution 请给出一个解决方案
You can change the root element name, pass in the serializer the XmlRootAttribute
parameter. 您可以更改根元素名称,在序列化器中传递
XmlRootAttribute
参数。
var human = new Human { name = "Smit" };
var xs = new XmlSerializer(typeof(Human), new XmlRootAttribute("Boy"));
using (var fs = new FileStream("test.xml", FileMode.Create))
xs.Serialize(fs, human);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.