Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:30:16+00:00 2026-06-13T17:30:16+00:00

I have a problem with deserialization with my logic simulation program. Here are my

  • 0

I have a problem with deserialization with my logic simulation program.

Here are my element classes:

public class AndGateData : TwoInputGateData
{

}

public class TwoInputGateData : GateData
{
    public TwoInputGateData()
    {
        Input2 = new InputData();
        Input1 = new InputData();
    }
    public InputData Input1 { get; set; }
    public InputData Input2 { get; set; }
}

public class GateData : ElementData
{
    public GateData()
    {
        OutputData = new OutputData();
    }

    public OutputData OutputData { get; set; }
}

public class ElementData
{
    public int Delay { get; set; }
    public Guid Id { get; set; }
}

And here are classes responsible for sockets:

public class InputData : SocketData
{

}

public class SocketData
{
    public Guid Id { get; set; }
    public SignalData SignalData { get; set; }
}

SignalData is not important here. So, I won’t write it (in order to keep this question clean) here unless somebody says it is necessary.

CircuitData is very important:

[XmlRoot("Circuit")]
public class CircuitData
{
    [XmlElement(typeof(AndGateData))]
    [XmlElement(typeof(OrGateData))]
    public List<ElementData> elements = new List<ElementData>();
    public List<WireData> wires = new List<WireData>();
    public void AddElement(ElementData element)
    {
        elements.Add(element);
    }
    public void AddWire(WireData wire)
    {
        wires.Add(wire);
    }
}

Wires are not important right now.

Now, I have written some Serialization:

public class CircuitDataWriter
{
     public static void Write(object obj, string fileName)
     {
         var xmlFormat = new XmlSerializer(typeof(CircuitData));
         using(Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None) )
         {
             xmlFormat.Serialize(fStream,obj);
         }
         Console.WriteLine("Circuit saved in XML format.");
     }

}

It works just like I wanted, it produces that xml document:

<?xml version="1.0"?>
-<Circuit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
-<AndGateData> 
<Delay>10</Delay> 
<Id>bfee6dd7-5946-4b7b-9d0b-15d5cf60e2bf</Id> 
-<OutputData> <Id>00000000-0000-0000-0000-000000000000</Id> </OutputData> 
-<Input1> <Id>7c767caf-79a9-4c94-9e39-5c38ec946d1a</Id> <SignalData xsi:type="SignalDataOn"/> </Input1> 
-<Input2> <Id>d2cad8f8-8528-4db3-9534-9baadb6a2a14</Id> <SignalData xsi:type="SignalDataOff"/> </Input2> 
</AndGateData> 
<wires/> 
</Circuit>

But I have problem with my DESERIALIZATION. Here is the code:

public static CircuitData Read()
{
    var reader = new XmlSerializer(typeof(CircuitData));
    StreamReader file = new StreamReader("Circuit.xml");
    var returnCircuitData = new CircuitData();
    returnCircuitData = (CircuitData) reader.Deserialize(file);
    return returnCircuitData;
}

Now, it deserializes my Circuit.xml to object, but this object only contains Id and Delay, it does not contain Input1, Input2 or Output. So, it is treated like Element, not like AndGate. I tried to solve it out for a day but it seems that no one has that kind of problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:30:18+00:00Added an answer on June 13, 2026 at 5:30 pm

    I have a suggestion for you, make the Write method generic like this and create the serializer using objectToSerialize.GetType():

    public static void Write<T>(T objectToSerialize, string fileName)
    {
        var xmlSerializer = new XmlSerializer(objectToSerialize.GetType());
        ...
    }
    

    The XmlSerializer.Deserialize() method returns object, you can make your Read method generic like this:

    public static T Read<T>(string fileName)
    {
        var serializer = new XmlSerializer(typeof(T));
        using (StreamReader file = new StreamReader(fileName))
        {
            return (T)serializer.Deserialize(file);
        }
    }
    

    Other than that you might want to read about:

    • XmlInclude that is used when you serialize derived classes.
    • XmlArray and XmlArrayItem that are used for controlling serialization of arrays
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a little problem implementing some serialization/deserialization logic. I have several classes that
I have the problem when sequentially serialize-deserialize-serialize a TestClass: [Serializable] public class TestClass {
Here is my problem. I have a bunch of Configuration classes that get serialized
I have a problem with deserialization in my silverlight project. I have class Obj
I have problem with context menu. I have got: @Override public void onCreateContextMenu(ContextMenu menu,
I have a problem with WCF deserialization where the client hangs on the response
I have a class like this: public class MyClass { private Queue<MyOtherClass> myQueue; }
PROBLEM: I have a Child class which uses DataContractSerialization and raises a Changed event
Could you guys help me I have a problem with deserialization via IXmlSerializable var
I have to implement ISerializable in a derived class (to do some custom serialization/deserialization)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.