private void ProcessTerm(XElement xmlNode, LocalTermContainer parentTermContainer, List <Guid> parentInOrderList, bool isTermLink) { LocalTerm term; Guid id = this.GetGuidAttributeValue(xmlNode, TaxmlSpec.IdToken) ?? Guid.Empty; if (isTermLink) { string nameHint = this.GetAttributeValue(xmlNode, TaxmlSpec.NameHintToken) ?? ""; string termLinkSourcePath = this.GetAttributeValue(xmlNode, TaxmlSpec.TermLinkSourcePathToken) ?? ""; if (id == Guid.Empty && string.IsNullOrWhiteSpace(termLinkSourcePath)) { throw new ParseException( "TermLink elements must have either the ID attribute or the TermLinkSourcePath attribute", xmlNode); } if (termLinkSourcePath.Length > 0) { term = LocalTerm.CreateTermLinkUsingPath(parentTermContainer.DefaultLanguageLcid, termLinkSourcePath); } else { term = LocalTerm.CreateTermLinkUsingId(id, parentTermContainer.DefaultLanguageLcid, nameHint); } } else { string name = this.GetRequiredAttributeValue(xmlNode, TaxmlSpec.NameToken); term = LocalTerm.CreateTerm(id, name, parentTermContainer.DefaultLanguageLcid); } this.ReadTaxmlComments(xmlNode, term); bool?isAvailableForTagging = this.GetBooleanAttributeValue(xmlNode, TaxmlSpec.IsAvailableForTaggingToken); if (isAvailableForTagging.HasValue) { term.IsAvailableForTagging = isAvailableForTagging.Value; } if (isTermLink) { bool?isPinnedRoot = this.GetBooleanAttributeValue(xmlNode, TaxmlSpec.IsPinnedRootToken); if (isPinnedRoot.HasValue) { term.IsPinnedRoot = isPinnedRoot.Value; } } else { string owner = this.GetAttributeValue(xmlNode, TaxmlSpec.OwnerToken); if (owner != null) { term.Owner = owner; } bool?isDeprecated = this.GetBooleanAttributeValue(xmlNode, TaxmlSpec.IsDeprecatedToken); if (isDeprecated.HasValue) { term.IsDeprecated = isDeprecated.Value; } } bool inOrder = this.GetBooleanAttributeValue(xmlNode, TaxmlSpec.InOrderToken) ?? false; if (inOrder) { if (term.Id == Guid.Empty) { throw new ParseException("The InOrder attribute cannot be used when the term ID is empty", xmlNode); } parentInOrderList.Add(term.Id); } // Add the term set to the parent term / term set parentTermContainer.AddTerm(term); var inOrderList = new List <Guid>(); foreach (XElement childNode in xmlNode.Elements()) { switch (childNode.Name.LocalName) { case TaxmlSpec.LocalizedDescriptionToken: int descriptionLcid = this.GetLcidFromLanguageAttribute(childNode, term); term.SetDescription(childNode.Value, descriptionLcid); break; case TaxmlSpec.CustomPropertyToken: this.ProcessCustomProperty(term.CustomProperties, childNode); break; case TaxmlSpec.LocalCustomPropertyToken: this.ProcessCustomProperty(term.LocalCustomProperties, childNode); break; case TaxmlSpec.CustomSortOrderToken: this.ProcessCustomSortOrder(term.CustomSortOrder, childNode); break; case TaxmlSpec.LabelToken: int labelLcid = this.GetLcidFromLanguageAttribute(childNode, term); bool?setAsDefaultLabel = this.GetBooleanAttributeValue(childNode, TaxmlSpec.IsDefaultForLanguageToken); term.AddLabel(childNode.Value, labelLcid, setAsDefaultLabel ?? false); break; case TaxmlSpec.TermToken: this.ProcessTerm(childNode, term, inOrderList, isTermLink: false); break; case TaxmlSpec.TermLinkToken: this.ProcessTerm(childNode, term, inOrderList, isTermLink: true); break; case TaxmlSpec.SyncActionToken: this.ProcessSyncAction(childNode, term); break; default: throw new ParseException("Unimplemented XML tag \"" + childNode.Name.LocalName + "\"", childNode); } } this.ProcessInOrderList(term, inOrderList, xmlNode); }
private static void ProcessCsvLines(LocalTermGroup termGroup, CsvReader csvReader) { int columnTermSetName = csvReader.GetColumnIndex("Term Set Name"); int columnTermSetDescription = csvReader.GetColumnIndex("Term Set Description"); int columnLcid = csvReader.GetColumnIndex("LCID"); int columnAvailableForTagging = csvReader.GetColumnIndex("Available for Tagging"); int columnTermDescription = csvReader.GetColumnIndex("Term Description"); int[] columnTermLabels = { csvReader.GetColumnIndex("Level 1 Term"), csvReader.GetColumnIndex("Level 2 Term"), csvReader.GetColumnIndex("Level 3 Term"), csvReader.GetColumnIndex("Level 4 Term"), csvReader.GetColumnIndex("Level 5 Term"), csvReader.GetColumnIndex("Level 6 Term"), csvReader.GetColumnIndex("Level 7 Term") }; LocalTermSet termSet = null; while (csvReader.ReadNextLine()) { // Is it a blank row? if (csvReader.Values.All(x => string.IsNullOrWhiteSpace(x))) { // Yes, skip it continue; } string termSetName = csvReader[columnTermSetName].Trim(); string termSetDescription = csvReader[columnTermSetDescription].Trim(); string lcidString = csvReader[columnLcid].Trim(); int currentLcid; if (!int.TryParse(lcidString, out currentLcid)) { currentLcid = termGroup.DefaultLanguageLcid; } bool availableForTagging = true; if (!string.IsNullOrWhiteSpace(csvReader[columnAvailableForTagging])) { availableForTagging = bool.Parse(csvReader[columnAvailableForTagging]); } string termDescription = csvReader[columnTermDescription].Trim(); var termLabels = new List <string>(); for (int i = 0; i < columnTermLabels.Length; ++i) { int columnIndex = columnTermLabels[i]; if (columnIndex < csvReader.Values.Count) { string value = csvReader[columnIndex].Trim(); if (string.IsNullOrEmpty(value)) { break; } termLabels.Add(value); } } if (!string.IsNullOrEmpty(termSetName)) { termSet = termGroup.AddTermSet(Guid.Empty, termSetName); termSet.SetName(termSetName, currentLcid); termSet.Description = termSetDescription; } if (termSet != null) { LocalTermContainer parent = termSet; if (termLabels.Count == 0) { throw new Exception("Missing term label"); } foreach (string parentTermLabel in termLabels.Take(termLabels.Count - 1)) { LocalTerm newParent; if (!parent.Terms.TryGetByName(parentTermLabel, currentLcid, out newParent)) { throw new Exception("No match found for parent term \"" + parentTermLabel + "\""); } parent = newParent; } string termLabel = termLabels.Last(); LocalTerm term = parent.AddTerm(Guid.Empty, termLabel); term.SetName(termLabel, currentLcid); term.SetDescription(termDescription, currentLcid); term.IsAvailableForTagging = availableForTagging; } } }