private static void TestXmlOverridenSerialization() { foreach (var filepath in Directory.EnumerateFiles(ConfigurationManager.AppSettings["ConvertedFiles"])) { UserCategories2 cats = null; Console.WriteLine($"Reading File: {filepath}"); using (var reader = File.Open(filepath, FileMode.Open, FileAccess.Read)) { var dsrlzr = new XmlSerializer(typeof(UserCategories2)); cats = dsrlzr.Deserialize(reader) as UserCategories2; } var srType = typeof(Workbook2); var xOver = new XmlAttributeOverrides(); var ignoreAttrs = new XmlAttributes(); ignoreAttrs.XmlIgnore = true; //xOver.Add(srType, $"{nameof(UserCategories2.SiteFavsList)}", ignoreAttrs); xOver.Add(srType, $"{nameof(Workbook2.ShowTabs)}", ignoreAttrs); xOver.Add(srType, $"{nameof(Workbook2.Size)}", ignoreAttrs); xOver.Add(srType, $"{nameof(Workbook2.Size)}", ignoreAttrs); xOver.Add(srType, $"{nameof(Workbook2.Tags)}", ignoreAttrs); //xOver.Add(srType, $"{nameof(UserCategories2.SiteFavsList)}.{nameof(SiteCategories.Categories)}.{nameof(Category2.Workbooks)}.{nameof(Workbook2.Project)}", ignoreAttrs); //xOver.Add(srType, $"{nameof(UserCategories2.SiteFavsList)}.{nameof(SiteCategories.Categories)}.{nameof(Category2.Workbooks)}.{nameof(Workbook2.Owner)}", ignoreAttrs); var convertedFilepath = ConfigurationManager.AppSettings["ConversionFolder"] + "\\Testfiles\\" + filepath.Split('\\').Last(); Console.WriteLine($"Writing to file: {convertedFilepath}"); using (var writer = File.Open(convertedFilepath, FileMode.OpenOrCreate, FileAccess.Write)) { var srlzr = new XmlSerializer(typeof(UserCategories2), xOver); srlzr.Serialize(writer, cats); } } }
private static void ConvertXml() { foreach (var filepath in Directory.EnumerateFiles(ConfigurationManager.AppSettings["ConversionFolder"])) { UserCategories cats = null; UserCategories2 newcats = null; Console.WriteLine($"Reading File: {filepath}"); using (var reader = File.Open(filepath, FileMode.Open, FileAccess.Read)) { var dsrlzr = new XmlSerializer(typeof(UserCategories)); cats = dsrlzr.Deserialize(reader) as UserCategories; } newcats = new UserCategories2 { UserId = cats.UserId, UserName = cats.UserName, SiteFavsList = cats.SiteCategoriesList .Select(x => new SiteCategories { SiteId = x.SiteId, SiteName = x.SiteName, Categories = x.Categories .Select(y => new Category2 { Name = y.Name, IncludeAllFavs = y.IncludeAllFavs, IncludeAllWorkbooks = y.IncludeAllWorkbooks, Workbooks = y.Workbooks.Select(z => new Workbook2 { ContentUrl = z.ContentUrl, CreatedAt = z.CreatedAt, Id = z.Id, IsFavourite = z.IsFavourite, Name = z.Name, Owner = z.Owner, Project = z.Project, ShowTabs = z.ShowTabs, Size = z.Size, Tags = z.Tags, UpdatedAt = z.UpdatedAt }).ToList() }).ToList() } ).ToList() }; var convertedFilepath = ConfigurationManager.AppSettings["ConvertedFiles"] + "\\" + filepath.Split('\\').Last(); Console.WriteLine($"Writing to file: {convertedFilepath}"); using (var writer = File.Open(convertedFilepath, FileMode.OpenOrCreate, FileAccess.Write)) { var srlzr = new XmlSerializer(typeof(UserCategories2)); srlzr.Serialize(writer, newcats); } } }