Error reflecting type when trying to serialize

Last post 07-01-2009 12:11 PM by Dangermouse. 4 replies.

Sort Posts:

  • Error reflecting type when trying to serialize

    07-01-2009, 4:24 AM
    • Member
      64 point Member
    • Dangermouse
    • Member since 07-15-2005, 6:00 AM
    • Posts 189

     Hi, when I try and run this statement:

    XmlSerializer

    s = new XmlSerializer(typeof(PlayerXMLClass.Player));

    The XML is unusual in its structure:

    <?xml version=\"1.0\" encoding=\"utf-8\"?>
    <plyr xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
     <club>Arsenal</club>
        <plyrdets>
           <first>Cesc</first>
           <last>Fab</last>
        </plyrdets>
        <plyrdets>
           <dob>12/12/1988</dob>
           <position>midfielder</position>
        </plyrdets>
    </plyr>

    The inner exception is:

    The XML element 'plyrdets' from namespace is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.

  • Re: Error reflecting type when trying to serialize

    07-01-2009, 8:07 AM
    • Participant
      1,726 point Participant
    • richiej
    • Member since 09-25-2006, 10:24 AM
    • Posts 345

     Can you post your code for your class please, just wanted to see how you've defined your class

  • Re: Error reflecting type when trying to serialize

    07-01-2009, 10:09 AM
    • Member
      64 point Member
    • Dangermouse
    • Member since 07-15-2005, 6:00 AM
    • Posts 189

     

    [Serializable]
        [XmlRoot(ElementName = "plyr")] 
        public class Player
        {
            private string _club;
            private PlyrDetails _plyrDetails;
            private PlyrDetails2 _plyrDetails2;
    
            [XmlElement(ElementName = "club")]
            public string Club
            {
                get { return _club; }
                set { _club = value; }
            }
    
            [XmlElement(ElementName = "plyrdets")]
            public PlyrDetails PlyrDetails
            {
                get { return _plyrDetails; }
                set { _plyrDetails = value; }
            }
    
            [XmlElement(ElementName = "plyrdets")]
            public PlyrDetails2 PlyrDetails2
            {
                get { return _plyrDetails2; }
                set { _plyrDetails2 = value; }
            }
        }
    
        public class PlyrDetails
        {
            private string _firstName;
            private string _lastName;
    
            [XmlElement(ElementName = "first")]
            public string FirstName
            {
                get { return _firstName; }
                set { _firstName = value; }
            }
    
            [XmlElement(ElementName = "last")]
            public string LastName
            {
                get { return _lastName; }
                set { _lastName = value; }
            }
        }
    
        public class PlyrDetails2
        {
            private string _dob;
            private string _position;
    
            [XmlElement(ElementName = "dob")]
            public string Dob
            {
                get { return _dob; }
                set { _dob = value; }
            }
    
            [XmlElement(ElementName = "position")]
            public string Position
            {
                get { return _position; }
                set { _position = value; }
            }
        }


  • Re: Error reflecting type when trying to serialize

    07-01-2009, 12:01 PM
    • Participant
      1,726 point Participant
    • richiej
    • Member since 09-25-2006, 10:24 AM
    • Posts 345

    The answer lies in how you defined your class!

     

    .. you have "PlyrDetails" and "PlyrDetails2" - despite having the same xmelement name because they are different classes you will have 2 different elements in your xml document.

    Do you need 2 different player details classes or can you not have one? 

  • Re: Error reflecting type when trying to serialize

    07-01-2009, 12:11 PM
    • Member
      64 point Member
    • Dangermouse
    • Member since 07-15-2005, 6:00 AM
    • Posts 189

    Hi, thanks for the response.

    Unfortunately the way the XML is, I have to create two separate tags (plyrdets) but they have different contents. I understand that this is strange and probably a bad XML design however I need to create a serializable class to hold this information.

    I can add an attribute to the tag to distinguish between them. Do you know how I can serialize this XML design (original post) into a class please?

Page 1 of 1 (5 items)