zoukankan      html  css  js  c++  java
  • xmlSerializer属性的使用

    学习了XmlAttribute,XmlElement属性的定义和使用。

    Order类定义

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    
    namespace Artech.XmlSerializerDemos
    {
        [XmlRoot("Ord",Namespace="http://www.artech.com")]
        public class Order
        {
            private double _totalPrice;
    
            private Guid _id;
            [XmlAttribute(AttributeName = "Orderid")]
            public Guid ID
            {
                get;
                set;
            }
    
            private DateTime _date;
            [XmlAttribute(AttributeName = "OrderDate")]
            public DateTime Date
            {
                get;
                set;
            }
    
           // [XmlElement(Order=1)]
            public string Customer
            {
                get;
                set;
            }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace Artech.XmlSerializerDemos
    {
        class Program
        {
            static void Main(string[] args)
            {
                Order order = new Order()
                {
                    ID = Guid.NewGuid(),
                    Date = DateTime.Today,
                    Customer = "Foo",
                    ShipAddress = "airport address"
                };
                Serialize<Order>(order, @"E:Order.xml");
            }
    
            static void Serialize<T>(T instance, string fileName)
            {
                using (XmlWriter writer = new XmlTextWriter(fileName, Encoding.UTF8))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    serializer.Serialize(writer, instance);
                }
            }
        }
    }
    
    
    
    this._totalPrice = totalPrice;
            }
        }
    }

    调用代码

  • 相关阅读:
    一切都是对象
    对象入门
    同步计算输入的各个数的总和与平均值
    与时间有关的类Date,DateFormat,Calendar
    获取文件信息
    串行化
    分解
    高速缓存
    压缩
    MyCAT实现MySQL的读写分离
  • 原文地址:https://www.cnblogs.com/chinaagan/p/3567815.html
Copyright © 2011-2022 走看看