• 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
  • 相关阅读:
    关于 setColorFilter 和 PorterDuff.Mode
    下拉刷新 SwipRefreshLayout
    from athletelist import AthleteList出现红色下滑波浪线警告
    IndentationError: unindent does not match any outer indentation level
    定制数据对象2
    定制数据对象
    数据处理02
    数据处理
    python 工具箱
    嵌套列表的格式打印
  • 原文地址:https://www.cnblogs.com/wuming/p/1718242.html
Copyright © 2020-2023  润新知