By using DeserializeObject method you can generate an object,
and by using SerializeObject method you can generate an xml string of that object.
public object DeserializeObject(string toDeserialize, Type k)
{
XmlSerializer xmlSerializer = new XmlSerializer(k);
StringReader textReader = new StringReader(toDeserialize);
return xmlSerializer.Deserialize(textReader);
}
public string SerializeObject(object toSerialize, Type k)
{
XmlSerializer xmlSerializer = new XmlSerializer(k);
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
}
How to Call Serialize and Deserialize methods example:
if (Input.OperationType == XmlOperationType.SerializeObject)
{
Output.XmlResult = SerializeObject(Input.ObjectForXmlOperation, typeof(MyItem));
}
else if (Input.OperationType == XmlOperationType.DeserializeObject)
{
Output.ObjectResult = DeserializeObject(Input.ObjectXml, typeof(MyItem));
}
To Serialize and Deserialize an object don't forget following attributes
[Serializable]
[DataContract]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class MyItem {..}
No comments:
Post a Comment