简体   繁体   中英

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.

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

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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