实现IXmlSerializable接口遇到的问题, 对象数组DeSerialize时值返回一个元素的数组? 源码如下

smartcreater 2007-11-14 06:59:15
问题:某个类需要实现自己特有的XmlSerialize。 类是这样的:

public class Parameter :IXmlSerializable
{
#region IXmlSerializable 成员
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new Exception("The method or operation is not implemented.");
}

public void ReadXml(System.Xml.XmlReader reader)
{
reader.Read();
reader.ReadStartElement("Name");
if (!reader.IsEmptyElement)
{
this._name = reader.ReadString();
reader.ReadEndElement();
}
//...给其它成员赋值
}

public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteElementString("Name", this._name);
//...其它成员的值写入Xml 片断
}
#endregion
}

//测试代码
[TestMethod]
public void TestDeserialize()
{
Parameter[] ps = new Parameter[3];
ps[0] = new Parameter("name1", typeof(string), "Test");
ps[1] = new Parameter("name2", typeof(DateTime), "2007-11-13");
ps[2] = new Parameter("name3", typeof(DataTable), "abc");

//序列化ps对象
MemoryStream mStream = new MemoryStream();
XmlSerializer sz = new XmlSerializer(typeof(Parameter[]));
sz.Serialize(mStream, ps);
string s = System.Text.ASCIIEncoding.UTF8.GetString(mStream.ToArray());
//序列化结果s 显示序列化对象正常。格式如: <ArrayOfParameter><Parameter></Parameter>..</ArrayOfParameter>

//反序列化s
byte[] bts = System.Text.ASCIIEncoding.UTF8.GetBytes(s);
mStream = new MemoryStream(bts);
sz = new XmlSerializer(typeof(Parameter[]));
object o = sz.Deserialize(mStream);
ps = o as Parameter[];
//反序列化结果ps, 它只包含一个Paramter对象(name1的实列被正确反序列化),但应该包含3个对象的。
}
}

====================================
这个问题一直找不到原因,期待高手解答。
...全文
314 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimh 2009-09-02
  • 打赏
  • 举报
回复

reader.Read(); //有问题
reader.ReadStartElement("Name"); //有问题,有可能读过其他元素,reader是只进的,不能后退
if (!reader.IsEmptyElement)
{
this._name = reader.ReadString();
reader.ReadEndElement();
}
//...给其它成员赋值
真相重于对错 2009-09-01
  • 打赏
  • 举报
回复
writexml readxml 格式不对
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;
namespace testxmlsz
{
class Program
{
static void Main(string[] args)
{

myclass[] ms = { new myclass(1),new myclass(2),new myclass(3)};
XmlSerializer xsz = new XmlSerializer(typeof(myclass[]));
MemoryStream mstream = new MemoryStream();
xsz.Serialize(mstream, ms);
byte[] bs = mstream.ToArray();
string strxml = System.Text.Encoding.UTF8.GetString(bs,0,bs.Length);
byte[] bd = System.Text.Encoding.UTF8.GetBytes(strxml);
mstream =new MemoryStream(bd);
object o = xsz.Deserialize(mstream);

}
}
[Serializable]
public class myclass:System.Xml.Serialization.IXmlSerializable,System.Runtime.Serialization.ISerializable
{
public myclass() { a = 0; }
public myclass(int temp) { a = temp; }
public int a;
#region IXmlSerializable 成员

public System.Xml.Schema.XmlSchema GetSchema()
{
throw new Exception("The method or operation is not implemented.");
}

public void ReadXml(System.Xml.XmlReader reader)
{
this.a = System.Xml.XmlConvert.ToInt32(reader.ReadElementString());
}

public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteString(System.Xml.XmlConvert.ToString(a));

}

#endregion

#region ISerializable 成员

void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{

}

#endregion
}
}
kingppy 2009-08-24
  • 打赏
  • 举报
回复
请将ReadXml完整代码贴出为

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧