none
C# XML Serialization with custom type property RRS feed

  • Question

  • Hi all,

    i have a very special use case and i need your help :)

    I have the following xml:

    <?xml version="1.0" encoding="utf-8"?>
    <Main>
      <Workspace>
        <W p3:type="Office" CommonProperty="1" Type="Office" OfficeProperty="2" xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" />
        <W p3:type="Home" CommonProperty="1" Type="Home" HomeProperty="2" xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" />
      </Workspace>
    </Main>

    In general this looks good for me, but i don't want the serializer to add the type information automatically because i already have the type as a Property declared.

    Here is what i expect:

    <?xml version="1.0" encoding="utf-8"?>
    <Main>
    	<Workspace>
    		<W Type="Office" CommonProperty="1" OfficeProperty="2"/>
    		<W Type="Home" CommonProperty="1" HomeProperty="2"/>
    	</Workspace>
    </Main>

    And here are the classes for this structure:

    public class Main
        {
            public List<W> Workspace { get; set; }
    
            public Main()
            {
                Workspace = new List<W>();
                Workspace.Add(new Office());
                Workspace.Add(new Home());
            }
        }
        
    
        [XmlInclude(typeof(Office))]
        [XmlInclude(typeof(Home))]
        public class W
        {
            [XmlAttribute]
            public int CommonProperty { get; set; }
            [XmlAttribute]
            public string Type { get; set; }
    
            public W()
            {
                Type = this.GetType().Name;
                CommonProperty = 1;
            }
        }
    
        public class Office : W
        {
            [XmlAttribute]
            public int OfficeProperty { get; set; }
    
            public Office()
            {
                OfficeProperty = 2;
            }
        }
        public class Home : W
        {
            [XmlAttribute]
            public int HomeProperty { get; set; }
    
            public Home()
            {
                HomeProperty = 2;
            }
        }

    I hope it is clear what i want and hopefully someone knows the answer.
    I can not change the structure of the XML since it is given and it must be like that.
    So i really need to serialize and deserialze everything to this format.

    Thanks in advance for your help! :)


    Thursday, October 10, 2019 11:08 AM

Answers

  • I already solved this issue.

    I convert my xml to json, and from json i convert it to my classes:
    It seems that json is possible to convert this.

    Thanks for your help!
    • Marked as answer by hpposch Thursday, October 10, 2019 1:33 PM
    Thursday, October 10, 2019 1:33 PM

All replies

  • I think that is not possible because serializer needs that information to deserialize objects back to instances so that it knows what is Home and what is Office. So best option is to use custom reading and writing XML from your objects.

    Thursday, October 10, 2019 12:20 PM
  • Ok i see..
    What do you mean with custom reading and writting?
    Should i implement my custom logic to read and write this xml?

    Do you have any ideas how this can be done?
    • Edited by hpposch Thursday, October 10, 2019 12:38 PM
    Thursday, October 10, 2019 12:38 PM
  • I already solved this issue.

    I convert my xml to json, and from json i convert it to my classes:
    It seems that json is possible to convert this.

    Thanks for your help!
    • Marked as answer by hpposch Thursday, October 10, 2019 1:33 PM
    Thursday, October 10, 2019 1:33 PM