How to use C # deserialization with hierarchical class structure

I am trying to deserialize some XML files into some classes that have been simplified to the following:

[XmlRoot("person")]
[Serializable]
public class Person
{
    [XmlElement]
    public Toy Toy { get; set; }
}

[Serializable]
public class ActionMan : Toy
{
    [XmlElement("guns")]
    public string Guns;
}

[Serializable]

public class Doll : Toy
{
    [XmlElement("name")]
    public  String Name;
}

[XmlInclude(typeof(Doll))]
[XmlInclude(typeof(ActionMan))]
public class Toy
{
}

[TestFixture]
public class ToyTest
{
    [Test]
    public void testHierarchy()
    {
        String filePath = @"test\brother.xml";
        String sisfilePath = @"test\sister.xml";
        var serializer = new XmlSerializer(typeof(Person));
        Person brother = (Person)serializer.Deserialize(new FileStream(filePath, FileMode.Open));
        Person sister = (Person)serializer.Deserialize(new FileStream(sisfilePath, FileMode.Open));

        Assert.IsNotNull(brother);
        Assert.IsNotNull(sister);
        Assert.IsAssignableFrom(typeof(ActionMan),brother.Toy);
        Assert.IsAssignableFrom(typeof(Doll),sister.Toy);
    }
}

I want to use C # serialization (I know that I can use my deserializer), and I think that maybe there is just a certain tag that I don’t know about (and I'm sure I have extra tags).

here is one of the xml files:

<person>
  <doll>
    <name>Jill</name>
  </doll>
</person>

The error I get is "Expected: Assigned from" on the third statement

+3
source share
4 answers

The class should contain the attribute "doll" instead of the attribute "Toy", I mean the name. The XML node must have the same name as the -Casing attribute name.

+1

(, ActionMan ),

<person>
  <Toy xsi:type="ActionMan" />
</person>

, . , XML.

+1

, , , , . , Xml. Toy , , .

, Xml #, xsd.exe .xsd xsd mydata.xml, # xsd /c /l:cs mydata.xsd. , , .

xsd Microsoft.

+1

public class Person 
{

    public Toy toy
    {
        get
        {
            return (doll == null) ? (Toy)actionMan : (Toy)doll;
        }

    }


    public Doll doll;
    public ActionMan actionMan;
}


public class Toy 
{

}


public class Doll : Toy
{
    public String name;

}


public class ActionMan : Toy
{
    public String guns;
}

class Program
{
    static void Main(string[] args)
    {

        Person brother = new Person();
        ActionMan am = new ActionMan();
        am.guns = "Laser Beam";
        brother.actionMan = am;

        Person sister = new Person();
        Doll d = new Doll();
        d.name = "Jill";
        sister.doll = d;

        Serialize(brother, "brother.xml");
        Serialize(sister, "sister.xml");

        Person b = Deserialize("brother.xml");
        Person s = Deserialize("sister.xml");

        Console.WriteLine(((ActionMan)b.toy).guns);
        Console.WriteLine(((Doll)s.toy).name);
        Console.Read();
    }

    public static Person Deserialize(String filename)
    {
        var serializer = new XmlSerializer(typeof(Person));
        return (Person)serializer.Deserialize(new FileStream(filename, FileMode.Open));

    }

    public static void Serialize(Person p, String filename){
        Stream stream = File.Open(filename, FileMode.Create);
        XmlSerializer s = new XmlSerializer(typeof(Person));
        s.Serialize(stream, p);
        stream.Close();

    }

You can go from here. Remember that this is the case with attribute names. The serialization output I received is

brother.xml

<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <actionMan>
    <guns>Laser Beam</guns>
  </actionMan>
</Person>

sister.xml

<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <doll>
    <name>Jill</name>
  </doll>
</Person>

Output signal

Laser Beam
Jill
0
source

Source: https://habr.com/ru/post/1792278/


All Articles