public DataContractOrder(Guid id, DateTime date, DataContractProduct product, int quantity) { this._orderID = id; this._orderDate = date; this._product = product; this._quantity = quantity; }
static void SerializeViaDataContractSerializer() { DataContractProduct product = new DataContractProduct(Guid.NewGuid(), "Dell PC", "Xiamen FuJian", 4500); DataContractOrder order = new DataContractOrder(Guid.NewGuid(), DateTime.Today, product, 300); string fileName = _basePath + "Order.DataContractSerializer.xml"; using (FileStream fs = new FileStream(fileName, FileMode.Create)) { DataContractSerializer serializer = new DataContractSerializer(typeof(DataContractOrder)); using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs)) { serializer.WriteObject(writer, order); } } Process.Start(fileName); }
private static void DataContractToXmlSerialization() { var product = new DataContractProduct(Guid.NewGuid(), "productTestName", "china", 10.25); var order = new DataContractOrder(Guid.NewGuid(), DateTime.Now, product, 10); var baseDir = AppDomain.CurrentDomain.BaseDirectory; var fileName = "../xml.xml"; var fullFileName = Path.Combine(baseDir, fileName); using (var stream = new FileStream(fullFileName, FileMode.Create)) { var serializationTool = new DataContractSerializer(typeof(XmlOrder)); using (var writer = XmlDictionaryWriter.CreateTextWriter(stream)) { serializationTool.WriteObject(writer, order); } Process.Start(fullFileName); } }