XmlWriter/XmlReader示例代码

在Silverlight项目中,如果您想最大程度的减少xap包的大小,仅使用默认System.Xml命名空间下提供的功能来实现“XML序列化/反序列化”,恐怕XmlReader/XmlWriter将成为唯一选择了,下面是示例代码:

using System.IO;
using System.Text;
using System.Xml;
  
namespace slLib
{
    public class Person
    {
        private string _name = "";
  
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
  
  
        private int _age = 0;
  
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age        
        {
            get { return _age; }
            set { _age = value; }
        }
  
        /// <summary>
        /// 将Person实例转化为xml字符串
        /// </summary>
        /// <returns></returns>
        public string ToXmlString() 
        {
            MemoryStream ms = new MemoryStream();
            XmlWriter writer = XmlWriter.Create(ms);
  
            writer.WriteStartDocument();
                writer.WriteStartElement("data");
                    writer.WriteStartElement("person");                      
                          
                        writer.WriteStartElement("name");                       
                            writer.WriteAttributeString("type", "String");
                            writer.WriteValue(this.Name);
                        writer.WriteEndElement();
  
                        writer.WriteStartElement("age");
                            writer.WriteAttributeString("type", "Int32");
                            writer.WriteValue(this.Age);
                        writer.WriteEndElement();
  
                    writer.WriteEndElement();
                writer.WriteEndElement();
            writer.WriteEndDocument();
  
            writer.Flush();
            writer.Close();
            byte[] bytes = ms.ToArray();
            string result = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
            return result;
        }
  
        /// <summary>
        /// 从xml字符串给Person实例属性赋值
        /// </summary>
        /// <param name="xmlString"></param>
        public void FromXmlString(string xmlString) 
        {
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString));
            XmlReader reader = XmlReader.Create(ms);
  
             
            reader.Read();
            reader.ReadStartElement("data");
                reader.ReadStartElement("person");
                    string nameType = reader.GetAttribute("type");
                    string nameValue = reader.ReadElementContentAsString();
                    string ageType = reader.GetAttribute("type");
                    int ageValue = reader.ReadElementContentAsInt();
                reader.ReadEndElement();
            reader.ReadEndElement();
            reader.Close();
  
            this._name = nameValue;
            this._age = ageValue;
              
  
        }
  
          
    }
}
posted @ 2014-03-08 07:34  网络虫  阅读(425)  评论(0编辑  收藏  举报