Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XmlSerialier reflection error ??

XmlSerialier reflection error ??

Scheduled Pinned Locked Moved C#
helpdata-structurestutorialquestioncareer
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    babbelfisken
    wrote on last edited by
    #1

    Hi! Basically i want to serialize an array of objects Booking. That class has public fields of basic types like string,int. A very simple class containg information of the booking. I have managed to serialize/deserilize the array before but when i added a public field object of type 'MyButton' which inherits the System.Windows.Forms.Button class i got Reflection error of type Booking[] ? I tried using an [XmlIgnore] on the object not to serializr this but it doesn't work. Booking class: string name; ...; ...; public Booking(string name,...,...) { this.name = name; ...; ...; myButton = new MyButton(); } ...; I have a booking manager class that takes care of every booking and also instantiate the xmlSerializer object with the following line xmlSer = new XmlSerializer(typeof(Booking[])); As I said, it worked fine until I added the MyButton object to the Booking class! Why can't I add an object to my class with the property of XmlIgnore on it and serialize that object ? Can't get a grip of what reflection error really means too! Thankful for suggestion on how to solve this. I want to use this XmlSerializer cause it makes the job a lot easier but if i cant workaround this problem I may have to build an xmldocument by hand and save it and i try to avoid it.

    E D 2 Replies Last reply
    0
    • B babbelfisken

      Hi! Basically i want to serialize an array of objects Booking. That class has public fields of basic types like string,int. A very simple class containg information of the booking. I have managed to serialize/deserilize the array before but when i added a public field object of type 'MyButton' which inherits the System.Windows.Forms.Button class i got Reflection error of type Booking[] ? I tried using an [XmlIgnore] on the object not to serializr this but it doesn't work. Booking class: string name; ...; ...; public Booking(string name,...,...) { this.name = name; ...; ...; myButton = new MyButton(); } ...; I have a booking manager class that takes care of every booking and also instantiate the xmlSerializer object with the following line xmlSer = new XmlSerializer(typeof(Booking[])); As I said, it worked fine until I added the MyButton object to the Booking class! Why can't I add an object to my class with the property of XmlIgnore on it and serialize that object ? Can't get a grip of what reflection error really means too! Thankful for suggestion on how to solve this. I want to use this XmlSerializer cause it makes the job a lot easier but if i cant workaround this problem I may have to build an xmldocument by hand and save it and i try to avoid it.

      E Offline
      E Offline
      Eitsop
      wrote on last edited by
      #2

      When you say XmlIgnore, do you mean that you have applied the XmlIgnoreAttribute? i.e.

      /* This field will be ignored when serialized--
      unless it's overridden. */
      [XmlIgnoreAttribute]
      public string Comment;

      - Eitsop What we do not understand we do not possess. - Goethe.

      B M 2 Replies Last reply
      0
      • E Eitsop

        When you say XmlIgnore, do you mean that you have applied the XmlIgnoreAttribute? i.e.

        /* This field will be ignored when serialized--
        unless it's overridden. */
        [XmlIgnoreAttribute]
        public string Comment;

        - Eitsop What we do not understand we do not possess. - Goethe.

        B Offline
        B Offline
        babbelfisken
        wrote on last edited by
        #3

        What I have on my "public MyButton myBut;" is an [XmlIgnore] Could it be that the XmlSerializer have trouble serializing objects like Button,Label classes because they have to do something with Windows? Or perhaps that my object class is inheriting the Button object? From one confused guy..

        E 1 Reply Last reply
        0
        • B babbelfisken

          What I have on my "public MyButton myBut;" is an [XmlIgnore] Could it be that the XmlSerializer have trouble serializing objects like Button,Label classes because they have to do something with Windows? Or perhaps that my object class is inheriting the Button object? From one confused guy..

          E Offline
          E Offline
          Eitsop
          wrote on last edited by
          #4

          I have tried out this code. This ignores the button altogether and seems to work just fine:

          using System;
          using System.IO;
          using System.Xml.Serialization;
          using System.Windows.Forms;

          namespace SerialTest
          {
          // This is the class that will be serialized.
          public class Group
          {
          [XmlIgnoreAttribute]
          public System.Windows.Forms.Button myButton;

          // a group name or something
          public string GroupName;
          
          // a comment of some sort...
          public string Comment;
          
          public void SerializeObject(string filename)
          {
          // Create an XmlSerializer instance.
          XmlSerializer xSer = new XmlSerializer(typeof(Group));
          
          // Writing the file requires a TextWriter.
          TextWriter writer = new StreamWriter(filename);
          
          // Serialize the object and close the TextWriter.
          xSer.Serialize(writer, this);
          writer.Close();
          }
          

          }
          }

          To run this the example I used was a form with a button and the following code on the OnClick event:

          Group a = new Group();
          a.Comment="A sample comment";
          a.GroupName="A sample group name";
          a.myButton = this.button1;
          a.SerializeObject("c:\\test.xml");

          That worked.

          - Eitsop What we do not understand we do not possess. - Goethe.

          B 1 Reply Last reply
          0
          • E Eitsop

            I have tried out this code. This ignores the button altogether and seems to work just fine:

            using System;
            using System.IO;
            using System.Xml.Serialization;
            using System.Windows.Forms;

            namespace SerialTest
            {
            // This is the class that will be serialized.
            public class Group
            {
            [XmlIgnoreAttribute]
            public System.Windows.Forms.Button myButton;

            // a group name or something
            public string GroupName;
            
            // a comment of some sort...
            public string Comment;
            
            public void SerializeObject(string filename)
            {
            // Create an XmlSerializer instance.
            XmlSerializer xSer = new XmlSerializer(typeof(Group));
            
            // Writing the file requires a TextWriter.
            TextWriter writer = new StreamWriter(filename);
            
            // Serialize the object and close the TextWriter.
            xSer.Serialize(writer, this);
            writer.Close();
            }
            

            }
            }

            To run this the example I used was a form with a button and the following code on the OnClick event:

            Group a = new Group();
            a.Comment="A sample comment";
            a.GroupName="A sample group name";
            a.myButton = this.button1;
            a.SerializeObject("c:\\test.xml");

            That worked.

            - Eitsop What we do not understand we do not possess. - Goethe.

            B Offline
            B Offline
            babbelfisken
            wrote on last edited by
            #5

            Yeah this works I know. I think(hope i did) i mentioned it that mybutton is an object of class MyButton:System.Windows.Forms.Button What you have created is a "standard" button and i have a custom button that inherits System.Windows.Forms.Button. So i wonder if there is any problems with inherited classes using XmlSerializer ?

            1 Reply Last reply
            0
            • B babbelfisken

              Hi! Basically i want to serialize an array of objects Booking. That class has public fields of basic types like string,int. A very simple class containg information of the booking. I have managed to serialize/deserilize the array before but when i added a public field object of type 'MyButton' which inherits the System.Windows.Forms.Button class i got Reflection error of type Booking[] ? I tried using an [XmlIgnore] on the object not to serializr this but it doesn't work. Booking class: string name; ...; ...; public Booking(string name,...,...) { this.name = name; ...; ...; myButton = new MyButton(); } ...; I have a booking manager class that takes care of every booking and also instantiate the xmlSerializer object with the following line xmlSer = new XmlSerializer(typeof(Booking[])); As I said, it worked fine until I added the MyButton object to the Booking class! Why can't I add an object to my class with the property of XmlIgnore on it and serialize that object ? Can't get a grip of what reflection error really means too! Thankful for suggestion on how to solve this. I want to use this XmlSerializer cause it makes the job a lot easier but if i cant workaround this problem I may have to build an xmldocument by hand and save it and i try to avoid it.

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #6

              Well, I do not immediately see any problem :(, but why is that button declared as public anyway?


              "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

              1 Reply Last reply
              0
              • E Eitsop

                When you say XmlIgnore, do you mean that you have applied the XmlIgnoreAttribute? i.e.

                /* This field will be ignored when serialized--
                unless it's overridden. */
                [XmlIgnoreAttribute]
                public string Comment;

                - Eitsop What we do not understand we do not possess. - Goethe.

                M Offline
                M Offline
                Member_14886602
                wrote on last edited by
                #7

                Eitsop wrote:

                When you say XmlIgnore, do you mean that you have applied the XmlIgnoreAttribute? i.e.

                /* This field will be ignored when serialized--
                unless it's overridden. */
                [XmlIgnoreAttribute]
                public string Comment;

                Indeed you are right rakhi2020[.]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups