Answered by:
Returning List of Custom Types

Question
-
I am working on xml files using serializations and deserializations.
I need a method for deserializations.
It will take two parameters(ReturnType Type ,string XmlSourcePath) and return the list of Type.
Is it possible to do with a single method?
Thnx,
please, mark this as answer if it is THE answerSunday, October 9, 2011 7:09 PM
Answers
-
http://msdn.microsoft.com/en-us/library/ms731073.aspx
One word frees us of all the weight and pain of life: that word is love.- Marked as answer by Bob Wu-MTModerator Wednesday, October 19, 2011 2:30 AM
Sunday, October 9, 2011 8:05 PM -
use following method and cast your object instance!
void main() { object obj = methodName("XmlSourcePath"); //in example YourType objInstance = (YourType)obj; } public object methodName(string xmlSourcePath) { //deserialize your object! return yourDeserializedObjectInstance; }
Any fool can know. The point is to understand.(Albert Einstein)- Marked as answer by Bob Wu-MTModerator Wednesday, October 19, 2011 2:30 AM
Sunday, October 9, 2011 10:08 PM -
Hi,
I had a similar scenario:
My XML File is in a format:
<DocumentList> <Document> <DocumentTypeCode>UAP11</DocumentTypeCode> <DocumentDescription>Application Form</DocumentDescription> </Document> <Document> <DocumentTypeCode>UAP14</DocumentTypeCode> <DocumentDescription>Buy to Let App Form</DocumentDescription> </Document> </DocumentList>
And the following Classes:[Serializable] [XmlRoot("DocumentList")] public class DocumentList { public DocumentList() { ListElement=new List<Document>(); } [XmlElement("Document")] public List<Document> ListElement { get; set; } }
and
public partial class Document { public Document() { } [XMLElement] public string DocumentTypeCode{get;set;} [XMLElement] public string DocumentTypeDescription{get;set;} }
Make sure your XML files and Classes are in synch.Now you can use the following code to deserialize the xml's to Generic List:
public static T DesirializeMyXML<T>(string fileName) { XmlSerializer serializer = new XmlSerializer(typeof(T)); FileStream fs = new FileStream(fileName, FileMode.Open); XmlReader reader = new XmlTextReader(fs); var returnList = (T)serializer.Deserialize(reader); return returnList; }
If this colves your problem, mark this as answer, or helpfull if this helped.
Warm Regards, Deepak Pandit- Proposed as answer by Bob Wu-MTModerator Wednesday, October 12, 2011 8:57 AM
- Marked as answer by Bob Wu-MTModerator Wednesday, October 19, 2011 2:30 AM
Monday, October 10, 2011 10:33 AM
All replies
-
http://msdn.microsoft.com/en-us/library/ms731073.aspx
One word frees us of all the weight and pain of life: that word is love.- Marked as answer by Bob Wu-MTModerator Wednesday, October 19, 2011 2:30 AM
Sunday, October 9, 2011 8:05 PM -
use following method and cast your object instance!
void main() { object obj = methodName("XmlSourcePath"); //in example YourType objInstance = (YourType)obj; } public object methodName(string xmlSourcePath) { //deserialize your object! return yourDeserializedObjectInstance; }
Any fool can know. The point is to understand.(Albert Einstein)- Marked as answer by Bob Wu-MTModerator Wednesday, October 19, 2011 2:30 AM
Sunday, October 9, 2011 10:08 PM -
Hi,
I had a similar scenario:
My XML File is in a format:
<DocumentList> <Document> <DocumentTypeCode>UAP11</DocumentTypeCode> <DocumentDescription>Application Form</DocumentDescription> </Document> <Document> <DocumentTypeCode>UAP14</DocumentTypeCode> <DocumentDescription>Buy to Let App Form</DocumentDescription> </Document> </DocumentList>
And the following Classes:[Serializable] [XmlRoot("DocumentList")] public class DocumentList { public DocumentList() { ListElement=new List<Document>(); } [XmlElement("Document")] public List<Document> ListElement { get; set; } }
and
public partial class Document { public Document() { } [XMLElement] public string DocumentTypeCode{get;set;} [XMLElement] public string DocumentTypeDescription{get;set;} }
Make sure your XML files and Classes are in synch.Now you can use the following code to deserialize the xml's to Generic List:
public static T DesirializeMyXML<T>(string fileName) { XmlSerializer serializer = new XmlSerializer(typeof(T)); FileStream fs = new FileStream(fileName, FileMode.Open); XmlReader reader = new XmlTextReader(fs); var returnList = (T)serializer.Deserialize(reader); return returnList; }
If this colves your problem, mark this as answer, or helpfull if this helped.
Warm Regards, Deepak Pandit- Proposed as answer by Bob Wu-MTModerator Wednesday, October 12, 2011 8:57 AM
- Marked as answer by Bob Wu-MTModerator Wednesday, October 19, 2011 2:30 AM
Monday, October 10, 2011 10:33 AM