locked
Returning List of Custom Types RRS feed

  • 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 answer
    Sunday, October 9, 2011 7:09 PM

Answers

  • 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)
    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
    Monday, October 10, 2011 10:33 AM

All replies

  • 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)
    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
    Monday, October 10, 2011 10:33 AM