zoukankan      html  css  js  c++  java
  • Introducing XML Serialization

    The following items can be serialized using the XmLSerializer class:

    • Public read/write properties and fields of public classes

    • Classes that implement ICollection or IEnumerable

      NoteNote

      Only collections are serialized, not public properties.

    • XmlElement objects

    • XmlNode objects

    • DataSet objects

    example code for ICollection:

    using System;
    using System.IO;
    using System.Collections;
    using System.Xml.Serialization;
    
    public class Test{
        static void Main(){
            Test t = new Test();
            t.SerializeCollection("coll.xml");
        }
    
        private void SerializeCollection(string filename){
            Employees Emps = new Employees();
            // Note that only the collection is serialized -- not the 
            // CollectionName or any other public property of the class.
            Emps.CollectionName = "Employees";
            Employee John100 = new Employee("John", "100xxx");
            Emps.Add(John100);
            XmlSerializer x = new XmlSerializer(typeof(Employees));
            TextWriter writer = new StreamWriter(filename);
            x.Serialize(writer, Emps);
        }
    }
    public class Employees:ICollection{
        public string CollectionName;
        private ArrayList empArray = new ArrayList(); 
    
        public Employee this[int index]{
            get{return (Employee) empArray[index];}
        }
        
        public void CopyTo(Array a, int index){
            empArray.CopyTo(a, index);
        }
        public int Count{
            get{return empArray.Count;}
        }
        public object SyncRoot{
            get{return this;}
        }
        public bool IsSynchronized{
            get{return false;}
        }
        public IEnumerator GetEnumerator(){
            return empArray.GetEnumerator();
        }
    
        public void Add(Employee newEmployee){
            empArray.Add(newEmployee);
        }
    }
    
    public class Employee{
        public string EmpName;
        public string EmpID;
        public Employee(){}
        public Employee(string empName, string empID){
            EmpName = empName;
            EmpID = empID;
        }
    }

    the serialization xml looks like :

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfEmployee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Employee>
        <EmpName>John</EmpName>
        <EmpID>100xxx</EmpID>
      </Employee>
    </ArrayOfEmployee>

    DataSet XML Serialization Example:

    private void SerializeDataSet(string filename){
        XmlSerializer ser = new XmlSerializer(typeof(DataSet));
            
        // Creates a DataSet; adds a table, column, and ten rows.
        DataSet ds = new DataSet("myDataSet");
        DataTable t = new DataTable("table1");
        DataColumn c = new DataColumn("thing");
        t.Columns.Add(c);
        ds.Tables.Add(t);
        DataRow r;
        for(int i = 0; i<10;i++){
            r = t.NewRow();
            r[0] = "Thing " + i;
            t.Rows.Add(r);
        }
        TextWriter writer = new StreamWriter(filename);
        ser.Serialize(writer, ds);
        writer.Close();
    }

    XMLElement or XMLNode Xml Serialization code Example:

    private void SerializeElement(string filename){
        XmlSerializer ser = new XmlSerializer(typeof(XmlElement));
        XmlElement myElement= 
        new XmlDocument().CreateElement("MyElement", "ns");
        myElement.InnerText = "Hello World";
        TextWriter writer = new StreamWriter(filename);
        ser.Serialize(writer, myElement);
        writer.Close();
    }
    
    private void SerializeNode(string filename){
        XmlSerializer ser = new XmlSerializer(typeof(XmlNode));
        XmlNode myNode= new XmlDocument().
        CreateNode(XmlNodeType.Element, "MyNode", "ns");
        myNode.InnerText = "Hello Node";
        TextWriter writer = new StreamWriter(filename);
        ser.Serialize(writer, myNode);
        writer.Close();
    }

     you also can control the xml serialization by attribute. and the attributes which can control the xml serialization. 

  • 相关阅读:
    连接池
    Socket编程实践(2) --Socket编程导引
    自己定义JSTL函数
    用new和delete运算符进行动态分配和撤销存储空间
    Unix网络编程学习笔记之第12章 IPv4与IPv6的互操作性
    矩阵十题【五】 VOJ1049 HDU 2371 Decode the Strings
    Unity3d / 3ds max 模型分享站点
    浙大PAT考试1077~1080(2014上机复试题目)
    Java反射学习总结五(Annotation(注解)-基础篇)
    关于http和https淘宝支付宝跨域解决方法研究
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2576496.html
Copyright © 2011-2022 走看看