繁体   English   中英

C#:使用不同的类名序列化和反序列化XML

[英]C#: serialize and deserialize XML with different class name

我从sql server获取XML,并且可以使用适当的Human类反序列化它。

public class Human
{
public string name {get;set;}
}

在为name属性提供值之后,我想使用不同的根名称对其进行序列化,因为我想使用新的类名再次对其进行反序列化

public class Boy
{ 
public string name {get;set;}
}

请给出一个解决方案

您可以更改根元素名称,在序列化器中传递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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM