Hussain Mohammed
Hussain Mohammed

Reputation: 39

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

Upvotes: 1

Views: 642

Answers (1)

Alexander Petrov
Alexander Petrov

Reputation: 14231

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

Upvotes: 3

Related Questions