私はずっとインターネットで検索していましたが、助けを見つけることができませんでした。私の問題はserialize
にarraylist
ですxml
。まずデータベースからデータを収集し、次のように配列リストに割り当てます。
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