コード例 #1
0
        public void SerializationToStringWorksOK()
        {
            string xMLExpectedString = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<OrderedItem xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n  <ItemName>Widget</ItemName>\r\n  <Description>Regular Widget</Description>\r\n  <UnitPrice>2.3</UnitPrice>\r\n  <Quantity>10</Quantity>\r\n  <LineTotal>23.0</LineTotal>\r\n</OrderedItem>";

            OrderedItem orderedItem = new OrderedItem();
            orderedItem.ItemName = "Widget";
            orderedItem.Description = "Regular Widget";
            orderedItem.Quantity = 10;
            orderedItem.UnitPrice = (decimal)2.30;
            orderedItem.Calculate();
            string xMLString = XMLSerializer.XMLSerializeToString<OrderedItem>(orderedItem, null, null);
            Assert.AreEqual(xMLExpectedString, xMLString);
        }
コード例 #2
0
        public void SerializationDeserializationThroughAFileWorksOK()
        {
            string targetSerializedFilePath = "SerializeTest.xml";

            OrderedItem orderedItem = new OrderedItem();
            orderedItem.ItemName = "Widget";
            orderedItem.Description = "Regular Widget";
            orderedItem.Quantity = 10;
            orderedItem.UnitPrice = (decimal)2.30;
            orderedItem.Calculate();
            XMLSerializer.XMLSerializeToFile<OrderedItem>(orderedItem, null, null, targetSerializedFilePath);
            OrderedItem deserializedOrderedItem = XMLSerializer.XMLDeserializeFromFile<OrderedItem>(targetSerializedFilePath, null, null);
            Assert.AreEqual(orderedItem.ItemName, deserializedOrderedItem.ItemName);
            Assert.AreEqual(orderedItem.Description, deserializedOrderedItem.Description);
            Assert.AreEqual(orderedItem.Quantity, deserializedOrderedItem.Quantity);
            Assert.AreEqual(orderedItem.UnitPrice, deserializedOrderedItem.UnitPrice);
            File.Delete("SerializeTest.xml");
        }