コード例 #1
0
        static void CallDeSerialization()
        {
            StreamReader file = null;

            try
            {
                XmlSerializer reader = new XmlSerializer(typeof(CamereRecord));
                file = new StreamReader(@"CamereRecord.xml");
                CamereRecord obj = (CamereRecord)reader.Deserialize(file);
                Console.WriteLine("Camera Details After Deserilization:");
                Console.WriteLine("Model Name : " + obj.MakeModel);
                Console.WriteLine("Price : " + obj.Price);
            }
            catch (FileNotFoundException ex)
            { Console.WriteLine(ex.Message); }
            catch (Exception ex)
            { Console.WriteLine(ex.Message); }
            finally
            {
                if (file != null)
                {
                    file.Close();
                }
            }
        }
コード例 #2
0
        static void CallSerialization()
        {
            CamereRecord sXmlElement = new CamereRecord();

            Console.WriteLine("Enter Camara Detail : ");
            Console.Write("Model Name : ");
            sXmlElement.MakeModel = Console.ReadLine();
            Console.Write("Price      : ");
            sXmlElement.Price = Console.ReadLine();
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(CamereRecord));
            TextWriter    writer        = new StreamWriter(@"CamereRecord.xml");

            xmlSerializer.Serialize(writer, sXmlElement);
            writer.Close();
        }