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;
            }
        }
    }

    调用代码

  • 相关阅读:
    [HDU]1086You can Solve a Geometry Problem too
    [HDU]2161Primes
    [HDU]2098分拆素数和
    [HDU]1431素数回文
    [HDU]1527取石子游戏
    [HDU]2092整数解
    [HDU]1405The Last Practice
    [HDU]2565放大的X
    [HDU]1723Distribute Message
    [HDU]1208Pascal's Travels
  • 原文地址:https://www.cnblogs.com/chinaagan/p/3567815.html
Copyright © 2011-2022 走看看