Discussion How can I Serialize and Deserialize an Xml ?

  • Monday, January 07, 2013 3:58 AM
     
      Has Code

    Hello msdn,

    How can I serialize and deserialise a class object in the following format?

    <ViewModels>
      <ViewModel Name="OrderCustomerViewModel" Type="Grid">
        <Property Name="OrderId" Type="int32" Nullable="False" EditorType="TextBox"/>
        <Property Name="ShippedDate" Type="DateTime" Nullable="True" EditorType="TextBox"/>/>
        <Property Name="ShipName" Type="string" Nullable="True" EditorType="TextBox"/>/>
        <Property Name="CustomerCity" Type="string" Nullable="True"/>
        <Property Name="CustomerContactName" Type="string" Nullable="True"/>
      </ViewModel>
      <ViewModel Name="OrderViewModel" Type="Grid">
        <Property Name="OrderId" Type="int32" Nullable="False" EditorType="TextBox"/>
        <Property Name="ShippedDate" Type="DateTime" Nullable="True" EditorType="TextBox"/>/>
        <Property Name="ShipName" Type="string" Nullable="True" EditorType="TextBox"/>/>
      </ViewModel>
    </ViewModels>
    Could any one give me a sample application for this?



    salampv.07

All Replies

  • Monday, January 07, 2013 5:18 AM
     
      Has Code

    Hi try this code,

        public class OrderCustomerViewModel
        {
            public int OrderId;
            public DateTime ShippedDate;
            public string ShipName;
        }
        class Program
        {
            static void Main(string[] args)
            {
                OrderCustomerViewModel objorder = new OrderCustomerViewModel()
                {
                    OrderId = 1,
                    ShippedDate = DateTime.Now,
                    ShipName = "Test"
                };
                XmlSerializer xmlSerializer = new XmlSerializer(objorder.GetType());
                StringWriter Writer = new StringWriter();
                xmlSerializer.Serialize(Writer, objorder);
    
                Console.WriteLine(Writer.ToString());
                StringReader strReader = new StringReader(Writer.ToString());
                XmlSerializer xmlDeSerializer = new XmlSerializer(objorder.GetType());
    
                XmlTextReader XmlReader = new XmlTextReader(strReader);
                Object AnObject = xmlDeSerializer.Deserialize(XmlReader);
                Console.ReadLine();
            }
           
        }


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

  • Monday, January 07, 2013 2:08 PM
     
      Has Code

    <Property Name="ShippedDate" Type="DateTime" Nullable="True" EditorType="TextBox"/>

    How can save the date in this format when I serialize an object? here Property is class, Name , Type, Nullable,EditorType are Properties of the class, I want to eleminate null value properties also.

    Could anyone help me please


    salampv.07

  • Monday, January 07, 2013 2:09 PM
     
     

    Thank you san Sanz, it does not meet the format requirement.

    Regards

    Salam


    salampv.07

  • Tuesday, January 08, 2013 3:55 AM
     
      Has Code

    Hi I dint get your requirement?

    Do you need to accept nulls? if that is the problem ,use DateTime?

       public class OrderCustomerViewModel
        {
            public int OrderId;
            public DateTime? ShippedDate;
            public string ShipName;
        }


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".