110,539
社区成员
发帖
与我相关
我的任务
分享
reader.Read(); //有问题
reader.ReadStartElement("Name"); //有问题,有可能读过其他元素,reader是只进的,不能后退
if (!reader.IsEmptyElement)
{
this._name = reader.ReadString();
reader.ReadEndElement();
}
//...给其它成员赋值
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
}
}