public IXmlSerializerConfiguration Apply()
        {
            if (_member == null && _target != null) _member = _target.Name;

            var map = new XmlMapper(_member, _targetType, _converter, _rootTarget);
            _mapper.Entries.Add(map);

            return _config;
        }
        public void CreateElementFromNullObject()
        {
            var astronautMapper = new XmlMapper<Astronaut>
                                      {
                                          {"name", a => a.Name},
                                      };

            Astronaut astronaut = null;

            var actual = astronautMapper.ToXml(astronaut, "astronaut");

            Assert.Equal("<astronaut />", actual.ToString());
        }
        public void CreatesRootElement()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"name", o => o.Name},
                             };

            var obj = new Spaceship {Name = "Apollo 11"};

            var actual = mapper.ToXml(obj, "spaceship");

            Assert.Equal("spaceship", actual.Name);
        }
        public void DoesNotOverwriteExistingObject()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"agencyName", o => o.Owner.Name},
                             };

            var xml = XElement.Parse(@"<spaceship agencyName=""NASA""/>");

            var original = new Spaceship {Owner = new Agency {Country = Country.USA}};
            var actual = mapper.ToObject(xml, original);

            Assert.Equal("NASA", actual.Owner.Name);
            Assert.Equal(Country.USA, actual.Owner.Country);
        }
        public void SetsCollectionItemFromElement()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"captainName", o => o.Crew[0].Name},
                                 {"pilotName", o => o.Crew[1].Name},
                             };

            var xml = XElement.Parse(@"<spaceship captainName=""Neil Armstrong"" pilotName=""Michael Collins""/>");

            var actual = mapper.ToObject(xml);

            Assert.Equal(2, actual.Crew.Count);
            Assert.Equal("Neil Armstrong", actual.Crew[0].Name);
            Assert.Equal("Michael Collins", actual.Crew[1].Name);
        }
        public void SetsCollectionFromElement()
        {
            var astronautMapper = new XmlMapper<Astronaut>
                                      {
                                          {"name", a => a.Name},
                                      };

            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"crew/member", o => o.Crew, astronautMapper},
                             };

            var xml = XElement.Parse(@"<spaceship><crew><member name=""Buzz Aldrin""/><member name=""Neil Armstrong""/></crew></spaceship>");

            var actual = mapper.ToObject(xml);

            Assert.Equal(2, actual.Crew.Count);
            Assert.Contains("Buzz Aldrin", actual.Crew.Select(c => c.Name));
            Assert.Contains("Neil Armstrong", actual.Crew.Select(c => c.Name));
        }
        public void CreateElementFromNullCollection()
        {
            var astronautMapper = new XmlMapper<Astronaut>
                                      {
                                          {"name", a => a.Name},
                                      };

            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"crew/member", o => o.Crew, astronautMapper},
                             };

            var spaceship = new Spaceship
            {
                Crew = null,
            };

            var actual = mapper.ToXml(spaceship, "spaceship");

            Assert.Equal("<crew />", actual.Element("crew").ToString());
        }
        public void SetsIntAttribute()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"year", o => o.Year},
                             };

            var obj = new Spaceship {Year = 1969};

            var actual = mapper.ToXml(obj, "spaceship");

            Assert.Equal("1969", actual.Attribute("year").Value);
        }
        public void SetsElementsFromCollection()
        {
            var astronautMapper = new XmlMapper<Astronaut>
                                      {
                                          {"name", a => a.Name},
                                      };

            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"crew/member", o => o.Crew, astronautMapper},
                             };

            var spaceship = new Spaceship
                                {
                                    Crew = new List<Astronaut>
                                               {
                                                   new Astronaut {Name = "Buzz Aldrin"},
                                                   new Astronaut {Name = "Neil Armstrong"},
                                               }
                                };

            var actual = mapper.ToXml(spaceship, "spaceship");

            var crew = actual.Element("crew");
            Assert.NotNull(crew);
            Assert.Equal(2, crew.Elements("member").Count());

            Assert.Contains("Buzz Aldrin", crew.Elements("member").Select(x => x.Attribute("name").Value));
            Assert.Contains("Neil Armstrong", crew.Elements("member").Select(x => x.Attribute("name").Value));
        }
        public void SetsElementFromComplexProperty()
        {
            var agencyMapper = new XmlMapper<Agency>
                                   {
                                       {"name", a => a.Name},
                                       {"country", a => a.Country},
                                   };

            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"agency", o => o.Owner, agencyMapper},
                             };

            var spaceship = new Spaceship
                                {
                                    Owner = new Agency
                                                {
                                                    Name = "NASA",
                                                    Country = Country.USA
                                                }
                                };

            var actual = mapper.ToXml(spaceship, "spaceship");

            var agency = actual.Element("agency");
            Assert.NotNull(agency);
            Assert.Equal("NASA", agency.Attribute("name").Value);
        }
        public void SetsDateTimeAttributeWithJustCustomParser()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"firstLaunch", o => o.FirstLaunch, s => DateTime.Parse(s), p => p.ToString("yyyy-MM-dd")},
                             };

            var obj = new Spaceship { FirstLaunch = new DateTime(1969, 7, 16) };

            var actual = mapper.ToXml(obj, "spaceship");

            Assert.Equal("1969-07-16", actual.Attribute("firstLaunch").Value);
        }
        public void SetsDateTimeAttribute()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"firstLaunch", o => o.FirstLaunch},
                             };

            var obj = new Spaceship { FirstLaunch = new DateTime(1969, 7, 16) };

            var actual = mapper.ToXml(obj, "spaceship");

            Assert.Equal(obj.FirstLaunch.ToString(CultureInfo.CurrentCulture), actual.Attribute("firstLaunch").Value);
        }
        public void SetsAttributesFromCollectionItems()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"captainName", o => o.Crew[0].Name},
                                 {"pilotName", o => o.Crew[1].Name},
                             };

            var spaceship = new Spaceship
                                {
                                    Crew = new List<Astronaut>
                                               {
                                                   new Astronaut {Name = "Neil Armstrong"},
                                                   new Astronaut {Name = "Michael Collins"},
                                               }
                                };

            var actual = mapper.ToXml(spaceship, "spaceship");

            Assert.Equal("Neil Armstrong", actual.Attribute("captainName").Value);
            Assert.Equal("Michael Collins", actual.Attribute("pilotName").Value);
        }
        public void SetsIndirectStringProperty()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"agencyName", o => o.Owner.Name},
                             };

            var xml = XElement.Parse(@"<spaceship agencyName=""NASA""/>");

            var actual = mapper.ToObject(xml);

            Assert.Equal("NASA", actual.Owner.Name);
        }
        public void SetsStringAttribute()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"name", o => o.Name},
                             };

            var obj = new Spaceship {Name = "Apollo 11"};

            var actual = mapper.ToXml(obj, "spaceship");

            Assert.Equal("Apollo 11", actual.Attribute("name").Value);
        }
        public void SetsStringProperty()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"name", o => o.Name},
                             };

            var xml = XElement.Parse(@"<spaceship name=""Apollo 11""/>");
            var actual = mapper.ToObject(xml);

            Assert.Equal("Apollo 11", actual.Name);
        }
        public void SetsStringAndInt32AndDateTimeProperty()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"name", o => o.Name},
                                 {"year", o => o.Year},
                                 {"launchDate", o => o.FirstLaunch, s => DateTime.Parse(s)},
                             };

            var xml = XElement.Parse(@"<spaceship name=""Apollo 11"" year=""1969"" launchDate=""16/7/1969""/>");

            var actual = mapper.ToObject(xml);

            Assert.Equal("Apollo 11", actual.Name);
            Assert.Equal(1969, actual.Year);
            Assert.Equal(new DateTime(1969,7,16), actual.FirstLaunch);
        }
        public void SetsPropertyFromTwoElements()
        {
            var mapper = new XmlMapper<Astronaut>
                             {
                                 {"fname", "lname", a => a.Name, (f,l) => string.Format("{0} {1}", f, l), s => Tuple.Create("","")},
                             };
            var xml = XElement.Parse(@"<astronaut fname=""Michael"" lname=""Collins""/>");

            var actual = mapper.ToObject(xml);

            Assert.Equal("Michael Collins", actual.Name);
        }
        public void SetsInt32Property()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"year", o => o.Year},
                             };

            var xml = XElement.Parse(@"<spaceship year=""1969""/>");
            var actual = mapper.ToObject(xml);

            Assert.Equal(1969, actual.Year);
        }
        public void SetsTwoAttributesFromProperty()
        {
            var mapper = new XmlMapper<Astronaut>
                             {
                                 {"fname", "lname", a => a.Name, (f,l) => string.Format("{0} {1}", f, l), s => Split(s)},
                             };

            var astronaut = new Astronaut {Name = "Michael Collins"};

            var actual = mapper.ToXml(astronaut, "astronaut");

            Assert.Equal("Michael", actual.Attribute("fname").Value);
            Assert.Equal("Collins", actual.Attribute("lname").Value);
        }
        public void SetsDateTimePropertyUsingCustomConverter()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"launchDate", o => o.FirstLaunch, s => DateTime.Parse(s)},
                             };
            var xml = XElement.Parse(@"<spaceship launchDate=""16/7/1969""/>");
            var actual = mapper.ToObject(xml);

            Assert.Equal(new DateTime(1969,7,16), actual.FirstLaunch);
        }
        public void SetsComplexPropertyFromElement()
        {
            var agencyMapper = new XmlMapper<Agency>
                                   {
                                       {"name", a => a.Name},
                                       {"country", a => a.Country},
                                   };

            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"agency", o => o.Owner, agencyMapper},
                             };

            var xml = XElement.Parse(@"<spaceship><agency name=""NASA"" country=""USA""/></spaceship>");

            var actual = mapper.ToObject(xml);

            Assert.Equal(Country.USA, actual.Owner.Country);
        }
        public void SetsIndirectEnumProperty()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"agencyCountry", o => o.Owner.Country},
                             };

            var xml = XElement.Parse(@"<spaceship agencyCountry=""USA""/>");

            var actual = mapper.ToObject(xml);

            Assert.Equal(Country.USA, actual.Owner.Country);
        }
        public void SetsAttributeFromIndirectStringProperty()
        {
            var mapper = new XmlMapper<Spaceship>
                             {
                                 {"agencyName", o => o.Owner.Name},
                             };

            var spaceship = new Spaceship
                                {
                                    Owner = new Agency
                                                {
                                                    Name = "NASA"
                                                }
                                };

            var actual = mapper.ToXml(spaceship, "spaceship");

            Assert.Equal("NASA", actual.Attribute("agencyName").Value);
        }
Example #25
0
 public void Setup()
 {
     this.mapper = ConstructMapper();
 }