我有一个奇怪的问题,很困惑。我想通过电线发送数据集并将其序列化。当我反序列化时,我想要检索对象及其属性。但是,当我DESERIALIZE(EDITED)时,似乎丢失了与基类关联的子类。我创建了以下类:
[Serializable]
public class DataAttributeColumn : DataColumn, ISerializable
{
private string attributeName;
[DataMember(IsRequired = true)]
public string AttributeName
{
get { return attributeName; }
set { attributeName = value; }
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("AttributeName", this.AttributeName);
}
}
我没有使用DataColumn的列名,而是想扩展它添加一个属性名称。这一切都100%有效,直到我通过电线发送。
我创建了以下Test类,并使用了Binary和DataContract序列化程序。
public class Test
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataAttributeColumn dac = new DataAttributeColumn();
public void CreateData()
{
ds.Tables.Add(dt);
dac.AttributeName = "MyAttribute";
dt.Columns.Add(dac);
}
public void SerializeBinary()
{
DataSet dss = new DataSet();
BinaryFormatter dcs = new BinaryFormatter();
ds.RemotingFormat = SerializationFormat.Xml;
string strTempfilePath = "binary.txt";
FileStream fs;
using (fs = new FileStream(strTempfilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
dcs.Serialize(fs, ds);
}
fs = new FileStream(strTempfilePath, FileMode.Open);
dss = dcs.Deserialize(fs) as DataSet;
DataAttributeColumn dac2 = null;
dac2 = dss.Tables[0].Columns[0] as DataAttributeColumn;
if (dac2 != null)
{
Console.WriteLine("YOU DID IT!!!!");
}
//Second Serialize
using (fs = new FileStream("binary2.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
dcs.Serialize(fs, dss);
}
}
public void SerializeDataContract()
{
DataSet dss = new DataSet();
Type[] knowntypes = { typeof(DataAttributeColumn), typeof(DataColumn) };
DataContractSerializer dcs = new DataContractSerializer(typeof(DataSet), knowntypes);
MemoryStream stream = new MemoryStream();
ds.RemotingFormat = SerializationFormat.Xml;
string strTempfilePath = "datacontract.txt";
FileStream fs;
using (fs = new FileStream(strTempfilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
dcs.WriteObject(fs, ds);
}
fs = new FileStream(strTempfilePath, FileMode.Open);
dss = dcs.ReadObject(fs) as DataSet;
DataAttributeColumn dac2 = null;
dac2 = dss.Tables[0].Columns[0] as DataAttributeColumn;
if (dac2 != null)
{
Console.WriteLine("YOU DID IT!!!!");
}
using (fs = new FileStream("datacontract2.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
dcs.WriteObject(fs, dss);
}
}
}
我的目标是创建一个属性,当我反序列化时,我将对象从DataColumn转换回DataAttributeColumn以便检索“AttributeName”。
我怎么能这样做?
编辑:序列化后的输出
<DataSet><xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Table1"><xs:complexType><xs:sequence><xs:element name="Column1" msdata:**AttributeName="MyAttribute"** type="xs:string" minOccurs="0"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"/></DataSet>
反序列化后输出和输出:
<DataSet><xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Table1"><xs:complexType><xs:sequence><xs:element name="Column1" type="xs:string" minOccurs="0"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"/></DataSet>
答案 0 :(得分:0)
密钥位于以下行:
Type[] knowntypes = { typeof(DataAttributeColumn), typeof(DataColumn) };
每次knowntypes
或Serialize
时,您需要使用这些Deserialize
。这样格式化程序将知道保存适当的实际类,即使包含它的数组是使用子类声明的。检查您自己的代码,您在一个地方正确使用它,但在其他地方没有!
答案 1 :(得分:0)
让我们尝试不同的方法。使用XmlSerializer:
XmlSerializer serializer = new XmlSerializer(typeof(DataSet));
FileStream fs = new FileStream(yourFilePath, FileMode.Create);
XmlWriterSettings Settings = new XmlWriterSettings(); //Please use this!
Settings.Indent = true;
Settings.IndentChars = " ";
Settings.NewLineHandling = NewLineHandling.None;
Settings.NewLineChars = "\n";
XmlWriter writer = XmlWriter.Create(fs, Settings);
//XmlWriter writer = new XmlTextWriter(fs, Encoding.ASCII);
serializer.Serialize(writer, ds);
writer.Close();
fs.Close();
反序列化使用:
XmlReader reader = new XmlTextReader(fs);
DataSet ds2 = (DataSet)serializer.Deserialize(reader);
reader.Close();