简体   繁体   中英

How to save a list of classes in combination with other variables in c# wpf

Hi I just started working with C# WPF and I read about Serialization to store or load your data. My Question is how can I store a class that contains a list of another class and some additional parameters?

My first class (MeterValues) contains a number of parameters (type,speed,etc..)

public class MeterValues{}

I now made a second class to store a list containing multiple instances of the first class type. (So if I have 3 different meters, this list size = 3)

public class MeterValuesList : IList<MeterValues>{}

Now I wish to add an additional parameter to the second class, something independent of the first class so it should only be saved once. (not for every instance of class1)

To make my problem clear, I could add the extra parameter to the first class, but then If I have 100 different meters, the parameter is stored 100 times, and I only need to store it once.

Any idea on how to do this?

PS: If you need any additional information please just ask, I'm very eager to learn and to assist you in helping me solve this problem. Thanks in advance.

UPDATE:

I'm able to save the class MeterValuesList to a .xml file but only the List gets stored in the file, the extra parameter does not show up (It is in the class right before I write it to the file, checked it with debugger but does not show up in the file)

MeterValuesList meterValuesList = DataContext as MeterValuesList;
meterValuesList.CommSettings = "Com5:19200,8,n,1";

    FileStream stream = null;
    try
    {
        stream = new FileStream(filename, FileMode.Create, FileAccess.Write);
        XmlSerializer serializer = new XmlSerializer(typeof(MeterValuesList));
        serializer.Serialize(stream, meterValuesList);
        stream.Close();
    }

This is the result after saving the class to an xml file. The extra parameter is missing.

<?xml version="1.0"?>
<ArrayOfMeterValues xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MeterValues></MeterValues>
</ArrayOfMeterValues>

Unless you plan on overriding or expanding the functionality that an IList has in place, there is no reason to inherit from it in your MeterValuesList class. This feels like a case of using a "has a" relationship instead of an "is a" relationship.

Try this instead:

public class MeterValuesGroup
{
    List<MeterValues> MeterList { get; set; }
    int ExtraParameter { get; set; }
    // whatever additional parameters you need here.
}

If you do need to inherit from IList or IEnumerable, you can do something similar. However, in order to serialize this class correctly, you'll have to implement IXmlSerializable in MeterValues and MeterValuesList.

(Here is an excellent example of how this will look: Proper way to implement IXmlSerializable? )

public class MeterValuesList : IList<MeterValues>, IXmlSerializable
{
    MeterValues[] _MeterList { get; set; }
    string CommSettings = "Com5:19200,8,n,1";

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteAttributeString("CommSettings ", CommSettings );

        foreach (var mv in _MeterList)
        {
            // kind of a bad example, but hopefully you get the idea
            if (mv== null) 
                return;
            writer.WriteStartElement("MeterValues");
            mv.WriteXml(writer);
            writer.WriteEndElement();
        }
    }

You can add this property in the second class MeterValueList and serilize this class and when you deserialize it , that additional property will be assigned.

    MeterValueList m = new MeterValueList();
     m.AdditionalParameter = 100;
    MemoryStream memorystream = new MemoryStream(); 
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(memorystream, m);
    byte[] yourBytesToDb = memorystream.ToArray();
    //here you write yourBytesToDb to database


    //----------read from database---------------------
    //here you read from database binary data into yourBytesFromDb
    MemoryStream memorystreamd = new MemoryStream(yourBytesFromDb);
    BinaryFormatter bfd = new BinaryFormatter();
    MeterValueList  md = bfd.Deserialize(memorystreamd) as MeterValueList ;
    var i  =  md.AdditinalParameter; // must print 100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM