// STEP 3: Update the term properties, including correcting any // temporary names that were introduced in step 1. static void ProcessPropertyUpdates(TermSetItem parentItem, ItemGoal parentItemGoal) { foreach (Term term in parentItem.Terms) { TermGoal termGoal = parentItemGoal.TermGoals.FirstOrDefault(t => t.Id == term.Id); if (termGoal == null) { continue; // This is a term that would have been deleted by the ProcessDeletes() method. } // ----------------- string goalName = TaxonomyItem.NormalizeName(termGoal.Name); if (term.Name != goalName) { Log("* Renaming term from \"" + term.Name + "\" to \"" + termGoal.Name + "\""); term.Name = goalName; } HashSet <string> labels = new HashSet <string>( term.Labels.Where(l => !l.IsDefaultForLanguage).Select(l => l.Value)); HashSet <string> labelsGoal = new HashSet <string>(termGoal.OtherLabels); // Delete any extra labels. foreach (string label in labels.Except(labelsGoal)) { Log("* Term \"" + term.Name + "\": Deleting label \"" + label + "\""); term.Labels.First(l => l.Value == label).Delete(); } // Add any missing labels. foreach (string label in labelsGoal.Except(labels)) { Log("* Term \"" + term.Name + "\": Adding label \"" + label + "\""); term.CreateLabel(label, lcid, isDefault: false); } if (term.GetDescription() != termGoal.Description) { Log("* Term \"" + term.Name + "\": Updating description"); term.SetDescription(termGoal.Description, lcid); } if (term.IsDeprecated != termGoal.IsDeprecated) { Log("* Term \"" + term.Name + "\": Marking as " + (termGoal.IsDeprecated ? "Deprecated" : "Not deprecated")); term.Deprecate(termGoal.IsDeprecated); } // ----------------- ProcessPropertyUpdates(term, termGoal); // Recurse. } }
// STEP 3: Update the term properties, including correcting any // temporary names that were introduced in step 1. static void ProcessPropertyUpdates(Wrapper parentWrapper, ItemGoal parentItemGoal) { foreach (Wrapper wrapper in parentWrapper.ChildTerms) { // Find the corresponding TermGoal object. TermGoal termGoal = parentItemGoal.TermGoals.First(t => t.Id == wrapper.Id); Term term = (Term)wrapper.Item; // ----------------- if (term.Name != termGoal.Name) { // Consider the TaxonomyItem.NormalizeName() method. Log("* Renaming term from \"" + term.Name + "\" to \"" + termGoal.Name + "\""); term.Name = termGoal.Name; } HashSet <string> labels = new HashSet <string>( term.Labels.ToList().Where(l => !l.IsDefaultForLanguage).Select(l => l.Value)); HashSet <string> labelsGoal = new HashSet <string>(termGoal.OtherLabels); // Delete any extra labels. foreach (string label in labels.Except(labelsGoal)) { Log("* Term \"" + term.Name + "\": Deleting label \"" + label + "\""); term.Labels.ToList().First(l => l.Value == label).DeleteObject(); } // Add any missing labels. foreach (string label in labelsGoal.Except(labels)) { Log("* Term \"" + term.Name + "\": Adding label \"" + label + "\""); term.CreateLabel(label, Program.lcid, isDefault: false); } if (wrapper.TermDescription.Value != termGoal.Description) { Log("* Term \"" + term.Name + "\": Updating description"); term.SetDescription(termGoal.Description, Program.lcid); } if (term.IsDeprecated != termGoal.IsDeprecated) { Log("* Term \"" + term.Name + "\": Marking as " + (termGoal.IsDeprecated ? "Deprecated" : "Not deprecated")); term.Deprecate(termGoal.IsDeprecated); } // ----------------- ProcessPropertyUpdates(wrapper, termGoal); // recurse } }
// STEP 2: Delete any leftover terms. static void ProcessDeletes(Wrapper parentWrapper, ItemGoal parentItemGoal) { foreach (Wrapper wrapper in parentWrapper.ChildTerms.ToList()) { TermGoal termGoal = parentItemGoal.TermGoals.FirstOrDefault(t => t.Id == wrapper.Id); if (termGoal == null) { Log("* Deleting extra term \"" + wrapper.Name + "\""); wrapper.Item.DeleteObject(); parentWrapper.ChildTerms.Remove(wrapper); } else { ProcessDeletes(wrapper, termGoal); // Recurse. } } }
// STEP 2: Delete any leftover terms. static void ProcessDeletes(TermSetItem parentItem, ItemGoal parentItemGoal) { foreach (Term term in parentItem.Terms) { TermGoal termGoal = parentItemGoal.TermGoals.FirstOrDefault(t => t.Id == term.Id); if (termGoal == null) { Log("* Deleting extra term \"" + term.Name + "\""); term.Delete(); term.TermStore.CommitAll(); } else { ProcessDeletes(term, termGoal); // recurse } } }
// This function uses the XmlSerializer object to write an example input file // that illustrates the XML syntax. static void GenerateExampleXmlFile() { TermSetGoal termSet = new TermSetGoal(); termSet.Name = "TermSet"; var parentTerm = new TermGoal() { Name = "Term1" }; termSet.TermGoals.Add(parentTerm); parentTerm.TermGoals.Add(new TermGoal() { Name = "Term2", OtherLabels = new List <string>(new[] { "A", "B", "C" }) }); XmlSerializer serializer = new XmlSerializer(typeof(TermSetGoal)); using (Stream stream = new FileStream("ExampleInput.xml", FileMode.Create)) { serializer.Serialize(stream, termSet); } }