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

    调用代码

  • 相关阅读:
    socket架构
    异常处理
    类的装饰器
    with&as上下文管理协议
    软件开发规范
    面向对象-描述符
    面向对象-迭代器
    面向对象编程多种特性
    体验Visual Studio 2015 之 MVC
    MVC 好记星不如烂笔头之 ---> 全局异常捕获以及ACTION捕获
  • 原文地址:https://www.cnblogs.com/chinaagan/p/3567815.html
Copyright © 2020-2023  润新知