public static T DeserializeFromText <T>(this String xml) { using (var stringReader = new StringReader(xml)) { using (var xmlStringReader = new XmlTextReader(stringReader)) { return(xmlStringReader.DeserializeFromStream <T>()); } } }
public void TestSerialization() { // arrange var test = Test.Create(); string tempFileName = Path.GetTempFileName(); // act XmlDocument serializedDocument; XmlElement serializedElement; Test deserializedFromStream, deserializedFromBytes, deserializedFromFile, deserializedFromText; try { serializedDocument = test.SerializeToDocument(); serializedElement = test.SerializeToElement(); test.SerializeToFile(tempFileName); using (var xmlReader = new XmlTextReader(tempFileName)) { deserializedFromStream = xmlReader.DeserializeFromStream <Test>(); } deserializedFromBytes = File.ReadAllBytes(tempFileName).Deserialize <Test>(); deserializedFromFile = tempFileName.DeserializeFromFile <Test>(); deserializedFromText = serializedDocument.OuterXml.DeserializeFromText <Test>(); } finally { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } } // assert Assert.AreEqual(serializedDocument.DocumentElement.OuterXml, serializedElement.OuterXml); Assert.AreEqual(test, deserializedFromStream); Assert.AreEqual(test, deserializedFromBytes); Assert.AreEqual(test, deserializedFromFile); Assert.AreEqual(test, deserializedFromText); }