Regress.arg
Regress.arg

Reputation: 473

C# program will not deserialize xml file

The context of this question is a milestone in a month long project. The big picture is to create a virtual interface between the video sensor software host and the peripheral it's targeting, so that certain hardware differences can be abstracted over.

Right now, I'm building up my classes and deserializing xml files containing the structure of the data my teams' project will be dealing with. This is the part of the code that deals with the deserialization:

MainWindow.xaml.cs (The main GUI class)

    private void DeserializeEdsSettings()
    {
        XmlSerializer serializer = new XmlSerializer(typeof(EDSRegisters));
        FileStream fs = new FileStream(EDSFileLocation, FileMode.Open);
        Statics.edsRegisters = (EDSRegisters) serializer.Deserialize(fs);
        fs.Close();
        Statics.EDSPackets = new ObservableCollection<EDSRegister>(); //Statics parameter.
        foreach (EDSRegister register in Statics.edsRegisters.storedRegisters)
        {
            Statics.EDSPackets.Add(register.DeepClone());
        }

    }

This is the basic class corresponding to the EDSRegister tag in the XML file below. This structure is the building block for the data I'm going to be working with.

[Serializable]
public class EDSRegister : INotifyPropertyChanged
{
    private bool _IsVisible = true;
    [System.Xml.Serialization.XmlIgnore]
    public bool IsVisible
    {
        get { return _IsVisible; }
        set
        {
            _IsVisible = value;
            NotifyPropertyChanged("IsVisible");
        }
    }
    private string _RegisterAddress = "Enter Register Value";
    private string RegisterAddress
    {
        get { return _RegisterAddress; }
        set 
        {
            _RegisterAddress = value;
            NotifyPropertyChanged("RegisterAddress");
        }
    }

This is the wrapper for the Collection for the EDSRegisters, and what the data is being Deserialized to. Note that the Count of the ObservableCollection persistently remains 0.

    public class EDSRegisters
    {
    [XmlArray("EDSRegisters")]
    [XmlArrayItem("EDSRegister")]
    public ObservableCollection<EDSRegister> storedRegisters = new ObservableCollection<EDSRegister>();
    public EDSRegisters()
    {
    }    
    }

This is the xml file I'm trying to deserialize

    <?xml version="1.0"?>
<EDSRegisters>
  <EDSRegister>
    <RegisterAddress>0</RegisterAddress>
  </EDSRegister>
  <EDSRegister>
    <RegisterAddress>1</RegisterAddress>
  </EDSRegister>
  <EDSRegister>
    <RegisterAddress>2</RegisterAddress>
  </EDSRegister>
  <EDSRegister>
    <RegisterAddress>3</RegisterAddress>
  </EDSRegister>
</EDSRegisters>

Note that I'd also like to know how I can go about debugging this issue for myself. I am a C# newcomer and have managed to glide along without much UnitTesting experience, which probably speaks volumes about my experience level.

I appreciate whatever help you can give me.

Upvotes: 0

Views: 105

Answers (1)

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22074

You can change your classes to this:

[XmlRoot("EDSRegister")]
public class EDSRegister {
}

[XmlRoot("EDSRegisters")]
public class EDSRegisters : ObservableCollection<EDSRegister> {
  public EDSRegisters() {
  }
}

Sample serialization:

var foo = @"<?xml version=""1.0""?>
<EDSRegisters>
  <EDSRegister>
    <RegisterAddress>0</RegisterAddress>
  </EDSRegister>
  <EDSRegister>
    <RegisterAddress>1</RegisterAddress>
  </EDSRegister>
  <EDSRegister>
    <RegisterAddress>2</RegisterAddress>
  </EDSRegister>
  <EDSRegister>
    <RegisterAddress>3</RegisterAddress>
  </EDSRegister>
</EDSRegisters>";

XmlSerializer serializer = new XmlSerializer(typeof(EDSRegisters));
var bar = serializer.Deserialize(new StringReader(foo));

Notes:

  • you're mixing viewmodel classes and serialization classes. XmlSerializer have its own rules (only public member/public classes with public constructor) so you may want to do decoupling.
  • [Serializable] has no effect on xml serializer - its for binary serialization only
  • if you want to play with xml serializer, I recommend opposite approach (design your classes for xml serialization, fill them with dummy data, serialize them and see how the xml output will look like)

Upvotes: 1

Related Questions