There were problems when deserializing the list. There are two PropertyGrid with Tube and Intersection classes with successors, each of which stores different configurations of different classes. And all this should be saved in one xml file, we only know that the PropertyGrid Tube value should be selected once, but there can be several PropertyGrid Intersection values. Here is the code that saves all the values, the check is done through foreach:

public bool Save(Tube m_tube, Intersection m_intersection, string m_fileName) { var settings = new XmlWriterSettings { Indent = true }; using (var writer = XmlWriter.Create(m_fileName, settings)) { writer.WriteStartElement("Part"); var ser = new XmlSerializer(typeof(Tube)); ser.Serialize(writer, m_tube); foreach (IntersectionListItem de in MainViewModel.GetInstance().IntersectionView._IntersectionRepository._Intersections) { ser = new XmlSerializer(typeof(Intersection)); ser.Serialize(writer, (Intersection)de._Intersection); } writer.WriteEndElement(); } return true; } 

The file is perfectly created. But now how to implement deserialization through a cycle? Here is the file that was created, one of the heirs of the Tube class and three heirs of the Intersection class were selected.

 <?xml version="1.0" encoding="utf-8"?> <Part> <Tube xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="OvalTube"> <Type>OvalTube</Type> <WallThickness>25</WallThickness> <Height>25</Height> <Width>25</Width> </Tube> <Intersection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="PlaneIntersection"> <Type>PlaneIntersection</Type> <Position>Middle</Position> <Cut>InnerCut</Cut> <Movement>Robot</Movement> <Burning>Marking</Burning> </Intersection> <Intersection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="TubeIntersection"> <Type>TubeIntersection</Type> <Position>Left</Position> <Cut>OuterCut</Cut> <Movement>RotationAxis</Movement> <Burning>Cutting</Burning> <_TubeDiameter>0</_TubeDiameter> </Intersection> <Intersection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ConeIntersection"> <Type>ConeIntersection</Type> <Position>Left</Position> <Cut>InnerCut</Cut> <Movement>Robot</Movement> <Burning>Cutting</Burning> </Intersection> </Part> 

I tried through the cycle but it does not work:

 public bool Load(string m_fileName) { using (var reader = XmlReader.Create(m_fileName)) { reader.ReadToFollowing("Tube"); var ser = new XmlSerializer(typeof(Tube)); MainViewModel.GetInstance().PartView._Tube = (Tube)ser.Deserialize(reader); ser = new XmlSerializer(typeof(Intersection)); MainViewModel.GetInstance().IntersectionView._Intersection = (Intersection)ser.Deserialize(reader); foreach (IntersectionListItem inter in MainViewModel.GetInstance().IntersectionView._IntersectionRepository._Intersections) MainViewModel.GetInstance().IntersectionView._IntersectionRepository.AddIntersection(inter); } return true; } 

Only Tube and the first created Intersection is read this way.

    1 answer 1

     MainViewModel.GetInstance().IntersectionView._Intersection = (Intersection)ser.Deserialize(reader); foreach (IntersectionListItem inter in MainViewModel.GetInstance().IntersectionView._IntersectionRepository._Intersections) MainViewModel.GetInstance().IntersectionView._IntersectionRepository.AddIntersection(inter); 

    First you deserialize one Intersection and save it to IntersectionView._Intersection , and then you try to loop through the IntersectionView._IntersectionRepository._Intersections list. Obviously, this list is empty, and as a result you get the only deserialized Intersection.

    In general, with such a codestyle, errors are made easily and naturally, rewrite it soon.

    • But as? Maybe there are ideas? Already rewrote this method several times and fail to save to the list during deserialization. - nigina_1989
    • while (serializer.CanDeserialize (reader)) {myIntersectionList.Add ((Intersection) serializer.Deserialize (reader)); } - nitrocaster