コード例 #1
0
        public void XmlSaveAndRestore()
        {
            var random = new Random(0);
            var saveds = new DemoPayload[3];

            for (var i = 0; i < saveds.Length; i++)
            {
                saveds[i] = new DemoPayload {
                    Text = i.ToString(), Number = random.Next(100, 1000)
                };
            }

            var builder = new StringBuilder();

            for (var i = 0; i < saveds.Length; i++)
            {
                var xml = XmlFragmentHelper.EncodeXmlFragment(saveds[i]);
                builder.AppendLine(xml);
            }

            var fileContent = builder.ToString();
            var restoreds   = XmlFragmentHelper.DecodeXmlFragments <DemoPayload>(fileContent);

            Assert.IsNotNull(restoreds);
            Assert.AreEqual(saveds.Length, restoreds.Length);
            for (var i = 0; i < saveds.Length; i++)
            {
                Assert.AreEqual(saveds[i].Text, restoreds[i].Text);
                Assert.AreEqual(saveds[i].Number, restoreds[i].Number);
            }
        }
コード例 #2
0
        public void SerializeXmlElement()
        {
            var ob = new DemoPayload {
                Text = "Some Text", Number = 42
            };

            var serializer = new XmlSerializer(ob.GetType());

            var builder = new StringBuilder();

            var namespaces = new XmlSerializerNamespaces();

            namespaces.Add(string.Empty, string.Empty);

            var settings = new XmlWriterSettings {
                OmitXmlDeclaration = true
            };

            using (var xmlWriter = XmlWriter.Create(builder, settings))
            {
                serializer.Serialize(xmlWriter, ob, namespaces);
            }

            var xml = builder.ToString();

            Assert.AreEqual("<DemoPayload Text=\"Some Text\" Number=\"42\" />", xml);
        }
コード例 #3
0
        public void SerializeXmlElementViaHelper()
        {
            var ob = new DemoPayload {
                Text = "Some Text", Number = 42
            };

            var xml = XmlFragmentHelper.EncodeXmlFragment(ob);

            Assert.AreEqual("<DemoPayload Text=\"Some Text\" Number=\"42\" />", xml);
        }