简体   繁体   中英

Conditional Xml deserialization in c#

i have a class which i am able to deserialize by getting xml from Sql server

public class personXML
{
public string lang{get;set;}
public string name_en{get;set;}
public string name_ar{get;set;}
}

i also have person class

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

i want that if lang="en" then person.name should be personXML.name_en and if lang="ar" then person.name should be personXML.name_ar. i want to do it by serialize personXML and again deserialize in person class please help

You won't be able to do that wihtout custom serialization: https://msdn.microsoft.com/en-us/library/ty01x675(v=vs.110).aspx

But giving your problem I would suggest a different approach. You could keep both personXML and person DTO in your code. In your Data Access Layer you only use personXML. But you expose repository returning person.

In other word, you keep the serialization easy, and you add an abstraction to manage the conversion between personXML and person into a dedicated repository.

Hope it helps.

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