序列化成XML

本文关键字:XML 序列化 | 更新日期: 2023-09-27 18:01:16

我一直在网上搜索,但找不到任何帮助。我的问题是serializearraylistxml。我首先从数据库中收集数据并将其分配给数组列表,如下所示:

    ArrayList conRc = new ArrayList();        
    while (readIp.Read())
    {
        string ipVal = readIp.GetString(0);
        string conLvlVal = readIp.GetString(1);
        string ispVal = readIp.GetString(2);
        string tsVal = readIp.GetString(3);
        ispVal = ispVal.Trim();
        ispVal = ispVal.Replace("'"", "");
        string localPortVal = readIp.GetString(4);
        string foeriegnGeoVal = readIp.GetString(5);

        conRc.Add(new Confidence(tsVal, ipVal, localPortVal, ispVal, foeriegnGeoVal, conLvlVal));

    }

并尝试按如下方式序列化数组列表,

    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList));
    System.IO.TextWriter writer = new System.IO.StreamWriter(@"F:'myItems.xml", false);
    serializer.Serialize(writer, conRc);
    writer.Close();

但是我得到一个错误,说

There was an error generating the XML document.

我可以知道如何执行这个任务吗?这会对我有很大的帮助。

供参考,下面是Confidence类,

public class Confidence
{
    private string ip;
    public string Ip
    {
        get { return ip; }
        set { ip = value; }
    }
    private string count;
    public string Count
    {
        get { return count; }
        set { count = value; }
    }
    private string isp;
    public string Isp
    {
        get { return isp; }
        set { isp = value; }
    }
    private string colColor;
    public string ColColor
    {
        get { return colColor; }
        set { colColor = value; }
    }
    private string timeStamp;
    public string TimeStamp
    {
        get { return timeStamp; }
        set { timeStamp = value; }
    }
    public string Port
    {
        get { return port; }
        set { port = value; }
    }
    public string ForeignGeo
    {
        get { return foreignGeo; }
        set { foreignGeo = value; }
    }
    private string port;
    private string foreignGeo;
    public Confidence(string timeStampVal, string ipVal, string portVal, string ispVal, string foreignGeoVal, string countVal)
    {
        this.timeStamp = timeStampVal;
        this.ip = ipVal;
        this.port = portVal;
        this.isp = ispVal;
        this.foreignGeo = foreignGeoVal;
        this.count = countVal;
    }

    public Confidence(string ipVal, string countVal, string ispVal, string colorVal, string timestampVal)
    {
        this.ip = ipVal;
        this.count = countVal;
        this.isp = ispVal;
        this.colColor = colorVal;
        this.timeStamp = timestampVal;
    }
    public Confidence(string ispVal)
    {
        this.isp = ispVal;
    }
}

编辑

以前的错误是由于缺少parameterless constructor,作为Alex Filipovici建议,但现在我得到一个新的错误如下,

[InvalidOperationException: The type Confidence was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.]
   System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType) +1151604
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterArrayList.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType) +465
   Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterArrayList.Write2_ArrayOfAnyType(Object o) +271
[InvalidOperationException: There was an error generating the XML document.]
   System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) +651
   System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces) +72
   System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o) +10
   Dashboard.getDataOutTable() in c:'Users'DELL'Documents'Visual Studio 2010'WebSites'Dashboard'Dashboard.aspx.cs:1035
   Dashboard.Page_Load(Object sender, EventArgs e) in c:'Users'DELL'Documents'Visual Studio 2010'WebSites'Dashboard'Dashboard.aspx.cs:59
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

序列化成XML

如果您看一下内部异常,您将看到您必须向Confidence类添加一个无参数构造函数:

public class Confidence
{
    public Confidence()
    {
    }
    // other class members
}
对于后续异常,尝试使用以下构造函数:
System.Xml.Serialization.XmlSerializer serializer = 
    new System.Xml.Serialization.XmlSerializer(typeof(ArrayList), 
    new Type[] { typeof(Confidence) });

第一个错误:没有默认构造函数

=>添加构造函数类:

public Confidence()
{
}

第二个错误:序列化器不能识别数组列表的类型。=> modify Serializer:

 System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList),
 new System.Type[] { typeof(Confidence) });