none
Protection Level RRS feed

  • Question

  • Hi,

    I do not understand why I'm getting protection level error?

    using System;
    using System.IO;
    using System.Xml.Serialization;
    namespace XmlSerialization
    {
        class Program
        {
            // Simple customer class 
            public class Customer
            {
                public string FirstName { get; set; }
                public string LastName { get; set; }
                public string EmailAddress { get; set; }
                // Overrides the Object.ToString() to provide a 
                // string representation of the object properties. 
                public override string ToString()
                {
                    return string.Format("{ 0} {1}\nEmail: {2}",
                        FirstName, LastName, EmailAddress);
                } 
            }
            
            static void Main(string[] args)
            {
                Customer c1 = new Customer 
                {
                    FirstName = "Orlando",
                    LastName = "Gee", 
                    EmailAddress = "orlando0@ hotmail.com"
                };
                XmlSerializer serializer = new XmlSerializer( typeof(Customer));
                StringWriter writer = new StringWriter();
                
                serializer.Serialize( writer, c1); 
                string xml = writer.ToString(); 
                
                Console.WriteLine(" Customer in XML:\n{0}\n", xml);
                Customer c2 = serializer.Deserialize( new StringReader( xml)) as Customer;
                Console.WriteLine(" Customer in Object:\n{0}", c2. ToString());
                
                Console.ReadKey();
            }
        }
    }

    Your help will be greatly appreciated.

    Thanks,


    wesley

    Friday, September 20, 2013 7:10 PM

Answers

  • Without testing: it's caused cause Customer is a nested class. Place it outside of the Program class.
    • Marked as answer by wes.r Friday, September 20, 2013 7:20 PM
    Friday, September 20, 2013 7:14 PM
  • Try this code

    using System;
    using System.IO;
    using System.Xml.Serialization;

    namespace ConsoleApplication1
    {
        class Program
        {
            // Simple customer class 


            static void Main(string[] args)
            {
                Customer c1 = new Customer
                {
                    FirstName = "Orlando",
                    LastName = "Gee",
                    EmailAddress = "orlando0@ hotmail.com"
                };
                XmlSerializer serializer = new XmlSerializer(typeof(Customer));
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, c1);
                string xml = writer.ToString();

                Console.WriteLine(" Customer in XML:\n{0}\n", xml);
                Customer c2 = serializer.Deserialize(new StringReader(xml)) as Customer;
                Console.WriteLine(" Customer in Object:\n{0}", c2.ToString());

                Console.ReadKey();
            }
        }
        public class Customer
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string EmailAddress { get; set; }
            // Overrides the Object.ToString() to provide a 
            // string representation of the object properties. 
            public override string ToString()
            {
                return string.Format("{0} {1} Email: {2}",
                    FirstName, LastName, EmailAddress);
            }
        }
    }


    Happy Coding, RDRaja

    • Marked as answer by wes.r Friday, September 20, 2013 7:21 PM
    Friday, September 20, 2013 7:15 PM

All replies

  • Without testing: it's caused cause Customer is a nested class. Place it outside of the Program class.
    • Marked as answer by wes.r Friday, September 20, 2013 7:20 PM
    Friday, September 20, 2013 7:14 PM
  • Try this code

    using System;
    using System.IO;
    using System.Xml.Serialization;

    namespace ConsoleApplication1
    {
        class Program
        {
            // Simple customer class 


            static void Main(string[] args)
            {
                Customer c1 = new Customer
                {
                    FirstName = "Orlando",
                    LastName = "Gee",
                    EmailAddress = "orlando0@ hotmail.com"
                };
                XmlSerializer serializer = new XmlSerializer(typeof(Customer));
                StringWriter writer = new StringWriter();

                serializer.Serialize(writer, c1);
                string xml = writer.ToString();

                Console.WriteLine(" Customer in XML:\n{0}\n", xml);
                Customer c2 = serializer.Deserialize(new StringReader(xml)) as Customer;
                Console.WriteLine(" Customer in Object:\n{0}", c2.ToString());

                Console.ReadKey();
            }
        }
        public class Customer
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string EmailAddress { get; set; }
            // Overrides the Object.ToString() to provide a 
            // string representation of the object properties. 
            public override string ToString()
            {
                return string.Format("{0} {1} Email: {2}",
                    FirstName, LastName, EmailAddress);
            }
        }
    }


    Happy Coding, RDRaja

    • Marked as answer by wes.r Friday, September 20, 2013 7:21 PM
    Friday, September 20, 2013 7:15 PM
  • this code had 2 problems

    1)nested class,place customer class outside the program

    2)space in { 0},change this to {0} just remove the space

     public override string ToString()
                {
                    return string.Format("{ 0} {1}\nEmail: {2}",
                        FirstName, LastName, EmailAddress);
                } 

    Happy Coding, RDRaja

    Friday, September 20, 2013 7:17 PM
  • Thanks...

    wesley

    Friday, September 20, 2013 7:21 PM
  • I was not aware that the placement of the Costumer class would've mattered.

    Thanks,

     

    wesley

    Friday, September 20, 2013 7:24 PM
  • The class must be public for the serializer. But cause its a nested class it has the visibility of class Program. As you haven't specified an explicit visibility modifier it has the default visibility of internal.
    Friday, September 20, 2013 7:29 PM