Bostwick
Bostwick

Reputation: 706

Serializer inside Serializer Object C#

I am not sure exactly what is the source of the error I am having so please request more detail if it will help. For some reason, when I try and deserialize my object not all the data is return(looking at the text file it all looks as if the data is there).

Here is the the things I think can id what the issue is(There is a lot of code on this project so a lot of things have been cut out to make it easier to read) :

[Serializable]
public class Unit
{
    public List<ModuleSlot> slotsInFront { get; set; }

    //this would never be reached because the module slot has a customer xml serial object that prevents the normal xml reader from reaching this
    public string some_fake_text{get;set;}
 }

public class ModuleSlot
{
    [XmlIgnore()]
    public StreamShape moduleShape { get; set; }

    [XmlElement("moduleShape")]
    public ShapeSerializer xmlModuleShape
    {
        get
        {
            if (moduleShape == null)
                return null;
            else
            {
                return new ShapeSerializer(moduleShape);
            }
        }
        set
        {
            moduleShape = value.getFirstShape();
        }
    }

}
public class ShapeSerializer : IXmlSerializable
{

    public void ReadXml(System.Xml.XmlReader reader)
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
        System.Xml.XmlReader subReader = XmlReader.Create(reader.ReadSubtree(), settings);
        subReader.ReadStartElement();
        while (subReader.Depth > 0)
        {
            xmlStreamShape newXmlShape = new xmlStreamShape();
            newXmlShape = (xmlStreamShape)new XmlSerializer(typeof(xmlStreamShape)).Deserialize(subReader);
            parameters.Add(newXmlShape.getShapeFromSaved());
            subReader.Read();
        }
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        foreach (StreamShape item in parameters)
        {
            xmlStreamShape convertedShape = new xmlStreamShape(item);
            new XmlSerializer(typeof(xmlStreamShape)).Serialize(writer, convertedShape, ns);
        }
    }
}

...

Upvotes: 1

Views: 189

Answers (1)

Bostwick
Bostwick

Reputation: 706

Found the issue in the xml writer. I moved away from a serialization on the shape so wont post an example of that on here but here is a method that had the same issue but has been resolved(feel free to post any issues you see with it):

public void ReadXml(System.Xml.XmlReader reader)
{
        //Skip whitespaces
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
        //Create a reader that will read the list's content
        System.Xml.XmlReader subReader = XmlReader.Create(reader.ReadSubtree(), settings);
        subReader.MoveToContent();
        subReader.ReadStartElement();
        while (subReader.Depth > 0)
        {
            Type type = Type.GetType(this.GetType().Namespace + "." + reader.Name);
            parameters.Add((Port)new XmlSerializer(type).Deserialize(subReader));
        }
        reader.ReadEndElement();
}

Upvotes: 1

Related Questions