zoukankan      html  css  js  c++  java
  • 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;
                
    
            }
    
            
        }
    }
    
    作者:菩提树下的杨过
    出处:http://yjmyzz.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    远程登陆服务——SSH
    A web-based 3D modeling framework for a runner-gate design( 一种基于web的三维建模框架)
    Vue+Element+Echarts+Springboot+微信小程序开发(肿瘤医学项目)
    基于拖放布局的 Twitter Bootstrap 网站生成器
    Ubuntu下git使用华为云/gitee/github
    在 Ubuntu 18.04 上安装 Postman
    在Ubuntu 18.04上安装Git与入门教程
    转载:渲染层和逻辑层
    Ubuntu系统中安装Neo4j
    NosqlBooster连接数据库失败connect ECONNREFUSED 127.0.0.1:27017——mongodb连接失败
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/2065273.html
Copyright © 2011-2022 走看看