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. 

  • 相关阅读:
    我们应该如何防范黑客的攻击? 有哪些棘手问题?
    德国网络安全公司Avira被收购,估值为1.8亿美元
    物联网会成为黑客攻击的目标,智慧城市如何才安全?
    因新型冠状病毒,笔记本电脑销售增长,人们寻求更好的设备进行远程工作
    从电脑维修工到互联网大佬,他是怎么做到的?解读郭盛华最真实的传奇生涯
    企业防御网络风险保护计划的5个步骤
    加载失败图片加样式
    请求接口无权限
    iview button根据条件 disabled可用或者不可用
    vue跨组件传值
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2576496.html
Copyright © 2011-2022 走看看