0

我一直在互联网上搜索,但找不到任何帮助。我的问题serializearraylistto xml。我首先从数据库中收集数据并将其分配给arraylist,如下所示,

    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));


    }

并尝试按如下方式序列化arraylist,

    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
4

2 回答 2

5

如果你看一下内部异常,你会发现你必须向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) });
于 2013-02-27T10:14:29.447 回答
2

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

=> 添加构造函数类:

public Confidence()
{
}

第二个错误:序列化程序无法识别 ArrayList 的类型。=> 修改序列化器:

 System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList),
 new System.Type[] { typeof(Confidence) });
于 2013-02-27T10:36:34.533 回答