学习了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; } } }
调用代码