How to convert XML data in to a class file using c#( de-serializing objects)
I have created a WCF Rest API Service using wcf.My service methods return multiple data type some methods returns a object of a class or collection of class, bool, int or collection type. so I planed to return a same Response in all methods for this I Create a new class as following:
[DataContract(Namespace = "")]
public class ServiceResponse
{
[DataMember]
public bool IsError
{ get; set; }
[DataMember]
public string ResponseMessage
{ get; set; }
[DataMember]
public Object ResponseData
{ get; set; }
}
In ServiceResponse Class I have declared an Object Type property ResponseData and my all method has return type of ServiceResponse.see below code :
public ServiceResponse GetUser(int userID)
{
User user = getUser(userID); get user data from database
ServiceResponse response=new ServiceResponse();
var response = new ServiceResponse
{
IsError = false,
ResponseMessage = string.Empty,
Respo开发者_C百科nseData = user; // Setting Result Data here
};
return response;
}
after calling service I am able to get response and de-serialize it to My return type class object(ServiceResponse)
var streamReader = new StreamReader(response.GetResponseStream());
var xSerializer = new XmlSerializer(typeof(ServiceResponse ));
var result = (ServiceResponse)xSerializer.Deserialize(streamReader);
the xml which i get from service is like as :
<RestResponse>
<IsError>false</IsError>
<ResponseData i:type="ArrayOfBlogEntryDTO">
<BlogEntryDTO>
<Active>0</Active>
<BlogEntryID>36</BlogEntryID>
<UserID>1</UserID>
<UserName>admin</UserName>
<Views>12</Views>
</BlogEntryDTO>
<BlogEntryDTO>
<Active>0</Active>
<BlogEntryID>36</BlogEntryID>
<UserID>1</UserID>
<UserName>admin</UserName>
<Views>12</Views>
</BlogEntryDTO>
</ResponseData>
<ResponseMessage></ResponseMessage>
as in ResponseData property I have got the array of a class object(Collection of class objects) I want to get it in a collection or object of user class.
similar like:
Collection<BlogEntryDTO> blogEntries=(Collection<BlogEntryDTO>) ResponseData;
How can i do this?
I don't think the DataContract and DataMember attributes are necessary here. You should read this article about using the XmlSerializer with WCF.
To deserialize your ResponseData object, you must first know what class type to deserialize it to. The DataContractSerializer adds markup to the XML so that it knows the type. I don't believe the XmlSerializer does this so you would have to supply an additional type name as another property in your ServiceResponse class. This also means that the first pass through won't be able to deserialize correctly so you'll likely need to serialize by hand on the server and send it down as a string and then deserialize it again by hand on the client to transform it to your User object.
You'd need to switch your ResponseData to a string object and base64 encode the serialized result (or something similar to that). And then you'd need the additional ResponseType string property.
So on the server, you'd do this:
using (var ms = new MemoryStream())
{
xmlSerializer.Serialize(ms, user);
response.ResponseData = Convert.ToBase64String(ms.ToArray());
response.ResponseType = user.GetType().FullName;
}
Then on the client, you'd do something like this:
var responseData = Convert.FromBase64String(result.ResponseData);
var responseType = Type.GetType(result.ResponseType);
var responseSerializer = new XmlSerializer(responseType);
using (var ms = new MemoryStream(responseData))
{
var user = responseSerializer.Deserialize(ms);
}
Then the user object should be the correct type.
Something like this?
[DataContract(Namespace = "")]
public class ServiceResponse<TObject>
{
[DataMember]
public bool IsError { get; set; }
[DataMember]
public string ResponseMessage { get; set; }
[DataMember]
public TObject ResponseData { get; set; }
}
And then to deserialize:
var streamReader = new StreamReader(response.GetResponseStream());
var xSerializer = new XmlSerializer(typeof(ServiceResponse<IEnumerable<BlogEntryDTO>>));
var result = (ServiceResponse<IEnumerable<BlogEntryDTO>>)xSerializer.Deserialize(streamReader);
精彩评论