Reputation: 2980
I was searching on the internet the whole while but could not find any help. My issue is to serialize
an arraylist
to xml
. I first gather data from the database and assign it to the arraylist as follows,
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));
}
and trying to serialize the arraylist as follows,
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();
but i get an error saying,
There was an error generating the XML document.
May I know how to perform this task please..it ll be a great help.
FYI, below is the Confidence
class,
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;
}
}
EDIT
Prevoiusly the error was due to the missing parameterless constructor
, as Alex Filipovici suggested, but now i get a new error as follows,
[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
Upvotes: 0
Views: 228
Reputation: 4104
First error : no default constructor in Confidence
=> Add in Constructor class :
public Confidence()
{
}
Second error : Serializer don't recognize type of ArrayList. => modify Serializer :
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList),
new System.Type[] { typeof(Confidence) });
Upvotes: 2
Reputation: 32541
If you take a look at the inner exception you'll see that you have to add a parameterless constructor to the Confidence
class:
public class Confidence
{
public Confidence()
{
}
// other class members
}
For the subsequent exception, try using this constructor:
System.Xml.Serialization.XmlSerializer serializer =
new System.Xml.Serialization.XmlSerializer(typeof(ArrayList),
new Type[] { typeof(Confidence) });
Upvotes: 5