public void WhenWriteWithOneSuperPerson_ThenWritesXml() { // Arrange List <SuperPerson> superPeople = new List <SuperPerson>() { new SuperPerson() { Id = 1, Name = "Superman", Allegiance = TeamNames.SuperFriends, Rank = 1, Health = 1000, Strength = 10, Resistance = 80, Intellect = 20, Speed = 60 }, }; // Note: Example of using complex XML string with TestDataLoader for expected // Note: Would skip in favor of TestDataLoader string expected = "<SuperPeople>" + "<SuperPerson Id='1' Name='Superman' Allegiance='Super Friends' Rank='1' Health='1000' Strength='10' Speed='60' Resistance='80' Intellect='20'></SuperPerson>" + "</SuperPeople>"; SuperXmlSerializer target = new SuperXmlSerializer(); XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.CloseOutput = false; writerSettings.Encoding = System.Text.Encoding.UTF8; writerSettings.Indent = false; writerSettings.OmitXmlDeclaration = false; // Act string actual; using (StringWriter stringWriter = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(stringWriter, writerSettings)) { target.Write(writer, superPeople); writer.Flush(); stringWriter.Flush(); actual = stringWriter.ToString(); } } // Assert AssertXml.AreEqual(expected, actual); }
public void WhenReadAndWrite_ThenRoundtripsXml() { // Arrange string inputXml = TestDataLoader.LoadText("TestData.SuperPeople.Valid.xml"); string expected = inputXml; SuperXmlSerializer target = new SuperXmlSerializer(); XmlReaderSettings settings = new XmlReaderSettings(); settings.CloseInput = false; settings.IgnoreWhitespace = true; settings.IgnoreComments = true; XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.CloseOutput = false; writerSettings.Encoding = System.Text.Encoding.UTF8; writerSettings.Indent = false; writerSettings.OmitXmlDeclaration = false; // Act IEnumerable <SuperPerson> superPeople; using (StringReader stringReader = new StringReader(inputXml)) { using (XmlReader reader = XmlReader.Create(stringReader, settings)) { superPeople = target.Read(reader); } } string actual; using (StringWriter stringWriter = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(stringWriter, writerSettings)) { target.Write(writer, superPeople); writer.Flush(); stringWriter.Flush(); actual = stringWriter.ToString(); } } // Assert AssertXml.AreEqual(expected, actual); }
private T DataContractRoundTrip <T>(T item, string expected) { var serializer = new DataContractSerializer(item.GetType()); string xml; using (var sw = new StringWriter()) using (var writer = XmlWriter.Create(sw, AssertXml.Settings)) { serializer.WriteObject(writer, item); writer.Flush(); xml = sw.ToString(); Debug.WriteLine("DataContractSerializer"); Debug.Write(xml); Debug.WriteLine(string.Empty); AssertXml.AreEqual(expected, xml); } using (var stringReader = new StringReader(xml)) using (var reader = XmlReader.Create(stringReader)) { return((T)serializer.ReadObject(reader)); } }
public void WhenWriteWithEmptyCollection_ThenCreatesXml() { // Arrange List <SuperPerson> superPeople = new List <SuperPerson>(); string expected = "<SuperPeople></SuperPeople>"; SuperXmlSerializer target = new SuperXmlSerializer(); XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.CloseOutput = false; writerSettings.Encoding = System.Text.Encoding.UTF8; writerSettings.Indent = false; writerSettings.OmitXmlDeclaration = false; string actual; using (StringWriter stringWriter = new StringWriter()) { // Act using (XmlWriter writer = XmlWriter.Create(stringWriter, writerSettings)) { target.Write(writer, superPeople); writer.Flush(); stringWriter.Flush(); actual = stringWriter.ToString(); } // Note: Example of using AssertXml for comparison // Assert AssertXml.AreEqual(expected, actual); } }
public void WhenWrite_ThenWritesXml() { // Arrange List <SuperPerson> superPeople = new List <SuperPerson>() { new SuperPerson() { Id = 1, Name = "Name1", Allegiance = "Allegiance1", Rank = 1, Health = 1, Strength = 1, Resistance = 1, Intellect = 1, Speed = 1 }, new SuperPerson() { Id = 2, Name = "Name2", Allegiance = "Allegiance2", Rank = 2, Health = 2, Strength = 2, Resistance = 2, Intellect = 2, Speed = 2 }, new SuperPerson() { Id = 3, Name = "Name3", Allegiance = "Allegiance3", Rank = 3, Health = 3, Strength = 3, Resistance = 3, Intellect = 3, Speed = 3 }, new SuperPerson() { Id = 4, Name = "Name4", Allegiance = "Allegiance4", Rank = 4, Health = 4, Strength = 4, Resistance = 4, Intellect = 4, Speed = 4 }, new SuperPerson() { Id = 5, Name = "Name5", Allegiance = "Allegiance5", Rank = 5, Health = 5, Strength = 5, Resistance = 5, Intellect = 5, Speed = 5 }, }; // Note: Example of using embedded resource XML with TestDataLoader for expected results string expected = TestDataLoader.LoadText("TestData.SuperPeople.Valid.xml"); SuperXmlSerializer target = new SuperXmlSerializer(); XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.CloseOutput = false; writerSettings.Encoding = System.Text.Encoding.UTF8; writerSettings.Indent = false; writerSettings.OmitXmlDeclaration = false; // Act string actual; using (StringWriter stringWriter = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(stringWriter, writerSettings)) { target.Write(writer, superPeople); writer.Flush(); stringWriter.Flush(); actual = stringWriter.ToString(); } } // Assert AssertXml.AreEqual(expected, actual); }