Answered How to Deserialize XML String

  • Friday, April 13, 2012 5:05 AM
     
      Has Code

    How to deserialize following string into Object of specific class.

    <UserManagement> <UserManage> <ActionType>1</ActionType> <User_ID>0</User_ID> <sCode>dtest12042012</sCode> <FirstName>dtestupdated-1</FirstName> <LastName>DTest</LastName> <Password>pune007</Password> <Email>a@y.in</Email> <CreateBy>1269</CreateBy> <NodeId>1000</NodeId> <Max_Login_Attempt>3</Max_Login_Attempt> <is_New_Account_Access>0</is_New_Account_Access> <Notify_Login_PswFail>0</Notify_Login_PswFail> <Notify_Login_UserFail>0</Notify_Login_UserFail> <ZoneID>51</ZoneID> <Default_Culture_ID>1</Default_Culture_ID> <User_Question>Your Pet Name?</User_Question> <User_Answer>ddc</User_Answer> <Group_ID>;76;</Group_ID> </UserManage> <UserManage> <ActionType>1</ActionType> <User_ID>0</User_ID> <sCode>dtest12042012</sCode> <FirstName>dtestupdated-1</FirstName> <LastName>DTest</LastName> <Password>pune007</Password> <Email>a@y.in</Email> <CreateBy>1269</CreateBy> <NodeId>1000</NodeId> <Max_Login_Attempt>3</Max_Login_Attempt> <is_New_Account_Access>0</is_New_Account_Access> <Notify_Login_PswFail>0</Notify_Login_PswFail> <Notify_Login_UserFail>0</Notify_Login_UserFail> <ZoneID>51</ZoneID> <Default_Culture_ID>1</Default_Culture_ID> <User_Question>Your Pet Name?</User_Question> <User_Answer>ddc</User_Answer> <Group_ID>;76;</Group_ID> </UserManage> <UserManage> <ActionType>1</ActionType> <User_ID>0</User_ID> <sCode>dtest12042012</sCode> <FirstName>dtestupdated-1</FirstName> <LastName>DTest</LastName> <Password>pune007</Password> <Email>a@y.in</Email> <CreateBy>1269</CreateBy> <NodeId>1000</NodeId> <Max_Login_Attempt>3</Max_Login_Attempt> <is_New_Account_Access>0</is_New_Account_Access> <Notify_Login_PswFail>0</Notify_Login_PswFail> <Notify_Login_UserFail>0</Notify_Login_UserFail> <ZoneID>51</ZoneID> <Default_Culture_ID>1</Default_Culture_ID> <User_Question>Your Pet Name?</User_Question> <User_Answer>ddc</User_Answer> <Group_ID>;76;</Group_ID> </UserManage> </UserManagement>




All Replies

  • Friday, April 13, 2012 5:11 AM
     
      Has Code

    You didnt ask a thing, so we dont actually know what would you like to do. But here is some brief explanation:

    Why would you deserialitze string to object? Object can be any type. So there is no need of boxing or anything else.

    Example:

    string str = "abc";
    int num = 1;
    object obj1 = str;
    object obj2 = num;


    Mitja

  • Friday, April 13, 2012 5:37 AM
     
     
  • Friday, April 13, 2012 9:36 AM
     
     

    use the attribute function for deserialize like -

    for more refer this link-

    http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document

  • Friday, April 13, 2012 10:26 AM
     
      Has Code

    Hi Dhaval, 

    You can also use XDocument with LINQ combination to retrieve back as objects, I will show simple example how to?

    Have a class structure

      public class UserManagement
            {
                public UserManagement()
                {
                    Managers = new List<UserManage>();
                }
                public List<UserManage> Managers { get; set; }
            }
    
            public class UserManage
            {
                public int ActionType { get; set; }
            }
     

    for simplicity I just considered only property ActionType

    and perform like, 

     var xml = @"<UserManagement>
    <UserManage>
    			<ActionType>1</ActionType>
    </UserManage>
    <UserManage>
    			<ActionType>2</ActionType>
    </UserManage>
    <UserManage>
    			<ActionType>3</ActionType>
    </UserManage>
    </UserManagement>";
    
    
      var xDoc = XDocument.Parse(xml, LoadOptions.PreserveWhitespace);
      var userManageList = from item in xDoc.Descendants("UserManage")
                           select new UserManage { ActionType = (int)item.Element("ActionType") };
    
      var userManagement = new UserManagement();
      userManagement.Managers.AddRange(userManageList.ToArray());


    I hope this helps you..


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

  • Saturday, April 14, 2012 4:10 AM
     
     

    Thanks for help. But,

    I need dynamic XML Deserialization ,I can't pass/write node element in string format.

    I'm not able to do this ("item.Element("ActionType")"). Because, I don't know Element Name


  • Saturday, April 14, 2012 6:24 AM
     
     Answered

    Hi Dhaval,

    You can generalize the implementation by using Reflection . If you provide more details like how you expect your input? How does you know which element matches to which property? ..etc

    We will be able to help you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".