xml序列化的例子
[System.ComponentModel.DefaultValueAttribute ("2002")]
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlAttribute xa = doc.Attributes["id"];
if (xa != null)
id = long.Parse(xa.Value);
xa = doc.Attributes["uri"];
if (xa != null)
uri = xa.Value;
xa = doc.Attributes["feedtype"];
if (xa == null)
feedtype = FeedSky.Services.feedtype.simple;
XmlNode xn=doc.SelectSingleNode("feed/title");
if (xn != null)
{
title=xn.Value;
}
xn = doc.SelectSingleNode("feed/tags");
if (xn != null)
{
tags = xn.Value;
}
XmlNodeList xns = doc.SelectNodes("feed/sourcerss");
if (xns != null && xns.Count > 0)
{
List<rss> srs = new List<rss>();
foreach(XmlNode x in xns)
{
rss r = new rss();
r.ReadXml(x.OuterXml);
srs.Add(r);
}
if (srs.Count > 0)
sourcerss = srs.ToArray();
}
}
public void WriteXml(XmlWriter writer)
{
// writer.WriteStartElement("feed");
if (id > 0)
{
writer.WriteAttributeString("id", id.ToString());
}
if (uri != null)
{
writer.WriteAttributeString("uri", uri);
}
if (feedtype != FeedSky.Services.feedtype.simple)
{
writer.WriteAttributeString("feedtype", feedtype.ToString());
}
if (title != null)
{
writer.WriteStartElement("title");
writer.WriteCData(title);
writer.WriteEndElement();
}
if (tags != null&&tags.Length>0)
{
writer.WriteStartElement("tags");
writer.WriteCData(tags);
writer.WriteEndElement();
}
if (sourcerss != null)
{
foreach (rss r in sourcerss)
{
writer.WriteStartElement("sourcerss");
r.WriteXml(writer);
writer.WriteEndElement();
}
}
// writer.WriteEndElement();
}
#endregion
public class feed:IXmlSerializable
{
public long id;
public string uri;
public string title;
public feedtype feedtype;
public string tags;
public rss[] sourcerss;
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlAttribute xa = doc.Attributes["id"];
if (xa != null)
id = long.Parse(xa.Value);
xa = doc.Attributes["uri"];
if (xa != null)
uri = xa.Value;
xa = doc.Attributes["feedtype"];
if (xa == null)
feedtype = FeedSky.Services.feedtype.simple;
XmlNode xn=doc.SelectSingleNode("feed/title");
if (xn != null)
{
title=xn.Value;
}
xn = doc.SelectSingleNode("feed/tags");
if (xn != null)
{
tags = xn.Value;
}
XmlNodeList xns = doc.SelectNodes("feed/sourcerss");
if (xns != null && xns.Count > 0)
{
List<rss> srs = new List<rss>();
foreach(XmlNode x in xns)
{
rss r = new rss();
r.ReadXml(x.OuterXml);
srs.Add(r);
}
if (srs.Count > 0)
sourcerss = srs.ToArray();
}
}
public void WriteXml(XmlWriter writer)
{
// writer.WriteStartElement("feed");
if (id > 0)
{
writer.WriteAttributeString("id", id.ToString());
}
if (uri != null)
{
writer.WriteAttributeString("uri", uri);
}
if (feedtype != FeedSky.Services.feedtype.simple)
{
writer.WriteAttributeString("feedtype", feedtype.ToString());
}
if (title != null)
{
writer.WriteStartElement("title");
writer.WriteCData(title);
writer.WriteEndElement();
}
if (tags != null&&tags.Length>0)
{
writer.WriteStartElement("tags");
writer.WriteCData(tags);
writer.WriteEndElement();
}
if (sourcerss != null)
{
foreach (rss r in sourcerss)
{
writer.WriteStartElement("sourcerss");
r.WriteXml(writer);
writer.WriteEndElement();
}
}
// writer.WriteEndElement();
}
#endregion
}
没有评论:
发表评论