• xml 序列化


    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;

    namespace SerializableTest
    {
        
    class Programaa
        {
           
            
    public void SerializeDocument()
            {
                
    string filename = "e:\\books.xml";
                
    // Creates a new XmlSerializer.
                XmlSerializer s =
                
    new XmlSerializer(typeof(MyRootClass));

                
    // Writing the file requires a StreamWriter.
                TextWriter myWriter = new StreamWriter(filename);

                
    // Creates an instance of the class to serialize. 
                MyRootClass myRootClass = new MyRootClass();

                
    /* Uses a more advanced method of creating an list:
             create instances of the Item and BookItem, where BookItem 
             is derived from Item. 
    */
                Item item1 
    = new Item();
                
    // Sets the objects' properties.
                item1.ItemName = "Widget1";
                item1.ItemCode 
    = "w1";
                item1.ItemPrice 
    = 231;
                item1.ItemQuantity 
    = 3;

                BookItem bookItem 
    = new BookItem();
                
    // Sets the objects' properties.
                bookItem.ItemCode = "w2";
                bookItem.ItemPrice 
    = 123;
                bookItem.ItemQuantity 
    = 7;
                bookItem.ISBN 
    = "34982333";
                bookItem.Title 
    = "Book of Widgets";
                bookItem.Author 
    = "John Smith";

                
    // Sets the class's Items property to the list.
                myRootClass.Items.Add(item1);
                myRootClass.Items.Add(bookItem);

                
    /* Serializes the class, writes it to disk, and closes 
                   the TextWriter. 
    */
                s.Serialize(myWriter, myRootClass);
                myWriter.Close();
            }

            
    public MyRootClass DeSerialize()
            {
                TextReader reader 
    = new StreamReader("e:\\books.xml");
                XmlSerializer serializer 
    =
               
    new XmlSerializer(typeof(MyRootClass));
                var myBooks 
    = (MyRootClass)serializer.Deserialize(reader);
                reader.Close();
                
    return myBooks;
            }

        }

        
    // This is the class that will be serialized.
        [Serializable]
        
    public class MyRootClass
        {
            
    public MyRootClass()
            {
                items 
    = new List<Item>();
            }

            
    private List<Item> items;

            [XmlArrayItem(ElementName 
    = "Item",
       IsNullable 
    = true,
       Type 
    = typeof(Item)),
       XmlArrayItem(ElementName 
    = "BookItem",
       IsNullable 
    = true,
       Type 
    = typeof(BookItem))]

            
    public List<Item> Items
            {
                
    get { return items; }
                
    set { items = value; }
            }
        }

        
    public class Item
        {
            [XmlElement(ElementName 
    = "OrderItem")]
            
    public string ItemName;
            
    public string ItemCode;
            
    public decimal ItemPrice;
            
    public int ItemQuantity;
        }

        
    public class BookItem : Item
        {
            
    public string Title;
            
    public string Author;
            
    public string ISBN;
        }



    }

    http://tech.ddvip.com/2010-03/1268192963146672_3.html

    http://msdn.microsoft.com/en-us/library/ms950721.aspx
  • 相关阅读:
    JAVA高级编程数据源datasource

    编写自己的JDBC框架
    libevent带负载均衡的多线程使用示例
    游戏数据分析-基本指标
    学习日记-----各种问题
    学习日记-----ORM
    【转】Delphi利用系统环境变量获取常用系统目录
    [转]Delphi多线程编程入门(二)——通过调用API实现多线程
    [转]Delphi多线程编程入门(一)
  • 原文地址:https://www.cnblogs.com/wuming/p/1718242.html
Copyright © 2020-2023  润新知