序列化为XML

时间:2013-02-27 10:04:49

标签: c# asp.net web-applications xml-serialization

我一直在互联网上搜索,但找不到任何帮助。我的问题是serialize arraylistxml。我首先从数据库中收集数据并将其分配给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;

    }
}

修改

Prevoiusly错误是由于缺少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

2 个答案:

答案 0 :(得分: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) });

答案 1 :(得分:2)

第一个错误:Confidence

中没有默认构造函数

=>添加构造函数类:

public Confidence()
{
}

第二个错误:Serializer无法识别ArrayList的类型。 =>修改Serializer:

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