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. 

  • 相关阅读:
    面试话痨(四)常量在哪里呀,常量在哪里
    面试话痨(三)我会锁的四种配法,您配吗?
    面试话痨(二)C:JAVA String,别以为你穿个马甲我就不认识你了
    面试话痨(一)让我们来热切的讨论这个养猪场吧
    (JAVA)String类型的逻辑语句编译
    小白的REDIS学习(二)-链表
    小白的Redis学习(一)-SDS简单动态字符串
    mongo中的游标与数据一致性的取舍
    spring-data-mongodb与mongo shell的对应关系
    spring-data-mongodb 使用原生aggregate语句
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2576496.html
Copyright © 2011-2022 走看看