C#Serialize包含更多列表的对象列表

时间:2017-09-15 08:11:57

标签: c# xml serialization

我在C#中遇到Xml.Serialization问题并希望得到帮助。我有一个MyObject列表,我想序列化。 MyObject类包含System.Windows.Forms.DataVisualization.Charting.DataPoint列表,但我没有获得MyObject序列化列表。

public class MyObject
{
    public Guid ID { get; set; }

    public string Name { get; set; }

    public bool Status { get; set; }

    public List<DataPoint> History { get; set; } = new List<DataPoint>();
}

我使用此代码序列化:

List<MyObject> lstObjects;

using (FileStream fileStrm = new FileStream([outputPath], FileMode.Create))
{
    XmlSerializer xmlSerial = new XmlSerializer(typeof(List<MyObject>));
    xmlSerial.Serialize(fileStrm, lstObjects);
}

但我在System.InvalidOperationException列表中找到了History。你有什么建议吗?

2 个答案:

答案 0 :(得分:2)

DataPoints包含几个不可(可直接)序列化的属性,如Colors,Fonts等。您可以为这些类型创建可序列化类型,也可以创建完全可序列化的DataPoint类,或者,如果您只需要一个子集,则创建一个可序列化的类,其中仅包含颜色的int和x&amp;的两个双精度类。 y值,可能是名称的字符串或工具提示..

以下是具有DataPoint属性的最小子集的可序列化类的示例:

[Serializable]
public class SPoint
{
    public int  PointColor { get; set; }
    public double XValue { get; set; }
    public double YValue { get; set; }
    public string Name { get; set; }

    public SPoint()        {        }

    public SPoint(int c, double xv, double yv,  string n)
    {
        PointColor = c; XValue = xv; YValue = yv; Name = n;
    }

    static public SPoint FromDataPoint(DataPoint dp)
    {
        return new SPoint(dp.Color.ToArgb(), dp.XValue, dp.YValues[0], dp.Name);
    }

    static public DataPoint FromSPoint(SPoint sp)
    {
        DataPoint dp = new DataPoint(sp.XValue, sp.YValue);
        dp.Color = Color.FromArgb(sp.PointColor);
        dp.Name = sp.Name;
        return dp;
    }
}

像这样使用:

using System.Xml.Serialization;
...
...
var points =  chart.Series[0].Points;

List<SPoint> sPoints = points.Cast<DataPoint>()
                             .Select(x => SPoint.FromDataPoint(x))
                             .ToList();

XmlSerializer xs = new XmlSerializer(sPoints.GetType());
using (TextWriter tw = new StreamWriter(@"yourfilename.xml"))
{
    xs.Serialize(tw, sPoints);
    tw.Close();
}

当然,反序列化反向也是如此:

using (TextReader tw = new StreamReader(@"yourfilename.xml"))
{
    //chart.Series[0].Points.Clear();
    //sPoints.Clear();
    sPoints = (List<SPoint>)xs.Deserialize(tw);
    tw.Close();
}
foreach (var sp in sPoints) s.Points.Add(SPoint.FromSPoint(sp));

答案 1 :(得分:1)

我在你提出的问题上创建了一个非常简单的例子。我将代码中的大部分内容作为相关示例,除了我作为列表实现的数据点。我能够序列化XML。希望这会有所帮助。

 class Program
{         
 static void Main(string[] args)
    {   
        Details details = new Details();
        details.ID = new Guid();
        details.Name = "testuser";
        details.Status = true;
        details.History = new List<DataPoint>();
        details.History.Add(new DataPoint() {Name = "test"});
        details.History.Add(new DataPoint() { Name = "test1" });
        details.History.Add(new DataPoint() { Name = "test2" });
        details.History.Add(new DataPoint() { Name = "test3" });
        Serialize(details);
        }


private static void Serialize(Details details)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Details));
        using (TextWriter writer = new StreamWriter(@"C:\Users\testuser\Desktop\Xml.xml"))
        {
            serializer.Serialize(writer, details);
        }
    }
}

public  class Details
{

    public Guid ID { get; set; }

    public string Name { get; set; }

    public bool Status { get; set; }

    public List<DataPoint> History { get; set; } = new List<DataPoint>();

}
public class DataPoint
{
    public string Name { get; set; }

}