locked
Map an XML into multiple objects, RRS feed

  • Question

  • User1699591550 posted

    There is an XML containing 50 elements. I need to map this XML in to multiple objects which have variable number of properties.

    like- One class conatin only 10 properties, second have 12 properties, third have 5 properties , etc.

    Thursday, March 12, 2015 6:34 AM

Answers

  • User1644755831 posted

    Hello Yogi Raj,

    For this issue you need to create following models.

    public class basicDetails
    {
     //properties
    }
    public class addressDetails
    {
    //properties
    }
    public class employmentDetails
    {
    //properties
    }
    public class nomineeDetails
    {
    //properties
    }
    public class personalDetails
    {
    public basicDetails basicdetails {get;set;}
      public addressDetails addressdetails {get;set;}
      public employmentDetails employementdetails {get;set;}
      public nomineeDetails nomineedetails {get;set;}

    }

    Now you can use System.Xml.Serialization.XmlSerializer to deserialize the XML data

    public void ReadXML()
    {
        System.Xml.Serialization.XmlSerializer reader = 
            new System.Xml.Serialization.XmlSerializer(typeof(personalDetails));
        System.IO.StreamReader file = new System.IO.StreamReader(
            @"c:\temp\yourfile.xml");
        Book overview = new Book();
        personalDetails details = (personalDetails)reader.Deserialize(file);
         //details will hold your data
    }
    

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 19, 2015 5:24 AM

All replies

  • User2008642861 posted

    Hi Yogi Raj,

    Thank you for your post. With your current imformation, it's hard for me to give you any useful suggestion. Could you please provide me more details about your requirement.

    I suppose that you may use Linq to XML to achieve your goal. Please check below article.

    http://weblogs.asp.net/dwahlin/using-linq-to-sql-xml-mapping-files-step-by-step

    Also, here is a similar case for you to refer as below link.

    http://stackoverflow.com/questions/87621/how-do-i-map-xml-to-c-sharp-objects

    About how to use Linq to XML, please refer to the below tutorial.

    http://www.dotnetcurry.com/showarticle.aspx?ID=564

    Hope this could be helpful to you.

    Best regards,

    Archer

    Thursday, March 12, 2015 10:52 PM
  • User1699591550 posted

    Hi Archer Wang,

    Thanks for your reply. You can consider these details for my question-

    Input XML-

    <Lead xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <personalDetails>
    <basicDetails>
    <title>1</title>
    <firstName>Kj</firstName>
    <middleName>Kjk</middleName>
    <lastName>Jkj</lastName>
    <dateOfBirth>1984-09-04</dateOfBirth>
    <gender>M</gender>
    <mobileNO>9999999999</mobileNO>
    </basicDetails>
    <addressDetails>
    <type>1</type>
    <houseNoAptNameSociety>asa</houseNoAptNameSociety>
    <roadAreaSector>asasa</roadAreaSector>
    <landmark>jk</landmark>
    <city>HYDERABAD</city>
    <state>AP</state>
    <pincode>999999</pincode>
    </addressDetails>
    <employmentDetails>
    <occupationType>Student</occupationType>
    <occupationCode>5555555555.00</occupationCode>
    <industryType>MIN</industryType>
    </employmentDetails>
    <nomineeDetails>
    <title>1</title>
    <firstName>Kj</firstName>
    <middleName>Kjk</middleName>
    <lastName>Jkj</lastName>
    </nomineeDetails>
    </personalDetails>
    </Lead>

    Now, I need to map this xml into Classes like-

    PersonalDetails, AddressDetails, EmployementDetails, NomineeDetails

    these classes have same properties as elements of xml.

    Tuesday, March 17, 2015 1:38 AM
  • User1644755831 posted

    Hello Yogi Raj,

    For this issue you need to create following models.

    public class basicDetails
    {
     //properties
    }
    public class addressDetails
    {
    //properties
    }
    public class employmentDetails
    {
    //properties
    }
    public class nomineeDetails
    {
    //properties
    }
    public class personalDetails
    {
    public basicDetails basicdetails {get;set;}
      public addressDetails addressdetails {get;set;}
      public employmentDetails employementdetails {get;set;}
      public nomineeDetails nomineedetails {get;set;}

    }

    Now you can use System.Xml.Serialization.XmlSerializer to deserialize the XML data

    public void ReadXML()
    {
        System.Xml.Serialization.XmlSerializer reader = 
            new System.Xml.Serialization.XmlSerializer(typeof(personalDetails));
        System.IO.StreamReader file = new System.IO.StreamReader(
            @"c:\temp\yourfile.xml");
        Book overview = new Book();
        personalDetails details = (personalDetails)reader.Deserialize(file);
         //details will hold your data
    }
    

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 19, 2015 5:24 AM
  • User1699591550 posted

    Hi Krunal,

    I appreciate your reply. But, I need to know if there is another custom way to do this without using inbuilt Serializer() and Deserializer() functions...

    Monday, March 23, 2015 8:46 AM