我自己定义了一个类,NamedArrayList,然后大致代码如下:
try
{
ArrayList arr = new ArrayList();
for(int i=0;i<2;i++)
{
// DataSeries dataSeries = new DataSeries();
// dataSeries.Name = "aa";
// arr.Add(dataSeries);
NamedArrayList n = new NamedArrayList();
n.Name = "aa";
arr.Add(n);
} StringWriter writer1 = new StringWriter();
XmlSerializer serializer1 = new XmlSerializer(arr.GetType());
serializer1.Serialize(writer1,arr);
}
catch(Exception eee)
{
throw eee;
}-------------
NameArrayList如下:
using System;
using System.Collections;
using System.Xml.Serialization;namespace Intel.Atm.Indicators.Common
{
[Serializable()]
[XmlInclude(typeof(DataPoint))]
[XmlInclude((typeof(DataSeries)))]
[XmlInclude(typeof (ArrayList))]
public class NamedArrayList
{
private string _name;
private ArrayList _arrayList = new ArrayList(); public NamedArrayList() {} public NamedArrayList(string name)
{
_name = name;
} public string Name
{
get { return _name; }
set { _name = value; }
} #region Collection Implementation
public ArrayList List
{
get { return _arrayList; }
}
public object this[int index]
{
get { return _arrayList[index]; }
}
public int Count
{
get { return _arrayList.Count; }
} public object SyncRoot
{
get { return this; }
} public bool IsSynchronized
{
get { return false; }
} public IEnumerator GetEnumerator()
{
return _arrayList.GetEnumerator();
} public void CopyTo(Array a, int index)
{
_arrayList.CopyTo(a, index);
}
public void Add(object newObject)
{
_arrayList.Add(newObject);
} public void Insert(int index, object newObject)
{
_arrayList.Insert(index, newObject);
} public virtual bool Contains(object item)
{
return _arrayList.Contains(item);
} public virtual bool Contains(DataPoint item)
{
return _arrayList.Contains(item);
} public override bool Equals(object obj)
{
bool objectsAreEqual = false; if (obj is NamedArrayList)
{
objectsAreEqual = Equals((NamedArrayList)obj);
}
else
{
objectsAreEqual = false;
} return objectsAreEqual;
} private bool Equals(NamedArrayList namedArrayList)
{
bool objectsAreEqual = false; if (this.Name == namedArrayList.Name)
{
objectsAreEqual = true;
}
else
{
objectsAreEqual = false;
} return objectsAreEqual;
} public void RemoveToEnd(int removeAfterIndex)
{
if(removeAfterIndex < _arrayList.Count)
_arrayList.RemoveRange(removeAfterIndex, (_arrayList.Count - removeAfterIndex > 0 ? _arrayList.Count - removeAfterIndex : 0));
} public void Sort(IComparer comparer)
{
_arrayList.Sort(comparer);
} public void Clear()
{
_arrayList.Clear();
} public override int GetHashCode()
{
return base.GetHashCode ();
} #endregion
}
}
执行抛出异常:{"There was an error generating the XML document." }
[System.InvalidOperationException]: {System.InvalidOperationException}
System.Object: {System.InvalidOperationException}
_className: null
_COMPlusExceptionCode: -532459699
_exceptionMethod: <undefined value>
_exceptionMethodString: null
_helpURL: null
_HResult: -2146233079
_innerException: {"The type Intel.Atm.Indicators.Common.NamedArrayList was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically." }
_message: "There was an error generating the XML document."
_remoteStackIndex: 0
_remoteStackTraceString: null
_source: null
_stackTrace: {System.Array}
_stackTraceString: null
_xcode: -532459699
_xptrs: 0
HelpLink: null
HResult: -2146233079
InnerException: {"The type Intel.Atm.Indicators.Common.NamedArrayList was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically." }
Message: "There was an error generating the XML document."
Source: "System.Xml"
StackTrace: " at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle)\r\n at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces)\r\n at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)\r\n at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)\r\n at Intel.Atm.Indicators.Common.VfApiTrendSnapshot.GetSnapshot(VfApiTrendModulePreference preference) in c:\\projects\\atm.indicators\\common\\vfapitrend\\vfapitrendsnapshot.cs:line 165"
TargetSite: {System.Reflection.RuntimeMethodInfo}
try
{
ArrayList arr = new ArrayList();
for(int i=0;i<2;i++)
{
// DataSeries dataSeries = new DataSeries();
// dataSeries.Name = "aa";
// arr.Add(dataSeries);
NamedArrayList n = new NamedArrayList();
n.Name = "aa";
arr.Add(n);
} StringWriter writer1 = new StringWriter();
XmlSerializer serializer1 = new XmlSerializer(arr.GetType());
serializer1.Serialize(writer1,arr);
}
catch(Exception eee)
{
throw eee;
}-------------
NameArrayList如下:
using System;
using System.Collections;
using System.Xml.Serialization;namespace Intel.Atm.Indicators.Common
{
[Serializable()]
[XmlInclude(typeof(DataPoint))]
[XmlInclude((typeof(DataSeries)))]
[XmlInclude(typeof (ArrayList))]
public class NamedArrayList
{
private string _name;
private ArrayList _arrayList = new ArrayList(); public NamedArrayList() {} public NamedArrayList(string name)
{
_name = name;
} public string Name
{
get { return _name; }
set { _name = value; }
} #region Collection Implementation
public ArrayList List
{
get { return _arrayList; }
}
public object this[int index]
{
get { return _arrayList[index]; }
}
public int Count
{
get { return _arrayList.Count; }
} public object SyncRoot
{
get { return this; }
} public bool IsSynchronized
{
get { return false; }
} public IEnumerator GetEnumerator()
{
return _arrayList.GetEnumerator();
} public void CopyTo(Array a, int index)
{
_arrayList.CopyTo(a, index);
}
public void Add(object newObject)
{
_arrayList.Add(newObject);
} public void Insert(int index, object newObject)
{
_arrayList.Insert(index, newObject);
} public virtual bool Contains(object item)
{
return _arrayList.Contains(item);
} public virtual bool Contains(DataPoint item)
{
return _arrayList.Contains(item);
} public override bool Equals(object obj)
{
bool objectsAreEqual = false; if (obj is NamedArrayList)
{
objectsAreEqual = Equals((NamedArrayList)obj);
}
else
{
objectsAreEqual = false;
} return objectsAreEqual;
} private bool Equals(NamedArrayList namedArrayList)
{
bool objectsAreEqual = false; if (this.Name == namedArrayList.Name)
{
objectsAreEqual = true;
}
else
{
objectsAreEqual = false;
} return objectsAreEqual;
} public void RemoveToEnd(int removeAfterIndex)
{
if(removeAfterIndex < _arrayList.Count)
_arrayList.RemoveRange(removeAfterIndex, (_arrayList.Count - removeAfterIndex > 0 ? _arrayList.Count - removeAfterIndex : 0));
} public void Sort(IComparer comparer)
{
_arrayList.Sort(comparer);
} public void Clear()
{
_arrayList.Clear();
} public override int GetHashCode()
{
return base.GetHashCode ();
} #endregion
}
}
执行抛出异常:{"There was an error generating the XML document." }
[System.InvalidOperationException]: {System.InvalidOperationException}
System.Object: {System.InvalidOperationException}
_className: null
_COMPlusExceptionCode: -532459699
_exceptionMethod: <undefined value>
_exceptionMethodString: null
_helpURL: null
_HResult: -2146233079
_innerException: {"The type Intel.Atm.Indicators.Common.NamedArrayList was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically." }
_message: "There was an error generating the XML document."
_remoteStackIndex: 0
_remoteStackTraceString: null
_source: null
_stackTrace: {System.Array}
_stackTraceString: null
_xcode: -532459699
_xptrs: 0
HelpLink: null
HResult: -2146233079
InnerException: {"The type Intel.Atm.Indicators.Common.NamedArrayList was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically." }
Message: "There was an error generating the XML document."
Source: "System.Xml"
StackTrace: " at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle)\r\n at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces)\r\n at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)\r\n at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)\r\n at Intel.Atm.Indicators.Common.VfApiTrendSnapshot.GetSnapshot(VfApiTrendModulePreference preference) in c:\\projects\\atm.indicators\\common\\vfapitrend\\vfapitrendsnapshot.cs:line 165"
TargetSite: {System.Reflection.RuntimeMethodInfo}
看样子八成是把不能转成ArrayList的变量强转了