Taxonomy Service and its output parsing code

The following code is created using this link. The code will need the Taxonomy Service ASMX file, termstore ID, term set ID.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Globalization;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
#region “Constants…”
const string TERMSET_ELEMNODE = “TS”;
const string TERMSETNAME_ATTR = “a12”;
const string TERMSETID_ATTR = “a9”;
const string TERM_ELEMNODE = “T”;
const string TERMID_ATTR = “a9”;
const string TERMMOREINFO_ELEMNODE = “TMS”;
const string TERMMOREINFOCHILD_ELEMNODE = “TM”;
const string PARENTTERMSETID_ATTR = “a24”;
const string LABELSET_ELEMNOE = “LS”;
const string LABELTERM_ELEMNOE = “TL”;
const string LABELNAME_ATTR = “a32”;
const string ISDEFAULTLABELNAME_ATTR = “a31”;
const string DESCRIPTIONS_ELEMNOE = “DS”;
const string TERMDESCRIPTION_ATTR = “TD”;
const string DESCRIPTIONTEXT = “a11”;
const string PARENTTERMID_ATTR = “a25”;
const string SETOFTERMSID_ATTR = “a45”;
#endregion
static void Main(string[] args)
{
ServiceReference1.TaxonomywebserviceSoapClient taxonomyClient = new ServiceReference1.TaxonomywebserviceSoapClient();
taxonomyClient.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
string result = null;
try
{
XElement termStoreIds = new XElement(“termStoreIds”,
new XElement(“termStoreId”, “35fa6bf3-f651-478a-a676-79d145c6aa09”));
XElement termSetIds = new XElement(“termSetIds”,
new XElement(“termSetId”, “ecc449d7-6370-4b43-8a7c-6d0c13668ee9”));
CultureInfo myCIintl = new CultureInfo(“en-US”, false);
XElement oldtimestamp = new XElement(“timeStamps”,
new XElement(“timeStamp”, “633992461437070000”));
XElement clientVersion = new XElement(“versions”,
new XElement(“version”, “1”));
string serverTermSetTimeStampXml = “”;
result = taxonomyClient.GetTermSets(
termStoreIds.ToString(SaveOptions.DisableFormatting),
termSetIds.ToString(SaveOptions.DisableFormatting),
myCIintl.LCID,
oldtimestamp.ToString(SaveOptions.DisableFormatting),
clientVersion.ToString(SaveOptions.DisableFormatting),
out serverTermSetTimeStampXml);
//
// Open the root file…
//
StringReader strReader = new StringReader(result);
XElement mainRoot = XElement.Load(strReader, LoadOptions.None);
ProcessRootElement ( mainRoot.Element(“TermStore”) );
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
#region “Helper methods..”
/// <summary>
///
/// </summary>
/// <param name=”root”></param>
public static void ProcessRootElement(XElement root)
{
//
// List all term sets
//
IEnumerable<XElement> termSets =
from termSet in root.Elements(TERMSET_ELEMNODE)
select termSet;
foreach (XElement el in termSets)
{
//
// Print term set name
//
PrintTermSetName(el);
//
// Enumberate all the child terms
//
IEnumerable<XElement> terms =
from term in root.Elements(TERM_ELEMNODE)
where (string)term.Element(TERMMOREINFO_ELEMNODE)
.Element(TERMMOREINFOCHILD_ELEMNODE)
.Attribute(PARENTTERMSETID_ATTR) == el.Attribute(TERMSETID_ATTR).Value
select term;
foreach (XElement el2 in terms)
{
//
// Check if it has parent element
//
if (IsParentTerm(el2))
{
//
// print the term name
//
PrintTermName(2, el2);
//
// Enumerate child terms from this term
//
string parentTermID = el2.Attribute(TERMID_ATTR).Value;
IEnumerable<XElement> terms1 =
from term in root.Elements(TERM_ELEMNODE)
where (string)term.Element(TERMMOREINFO_ELEMNODE)
.Element(TERMMOREINFOCHILD_ELEMNODE)
.Attribute(PARENTTERMID_ATTR) == parentTermID
select term;
foreach (XElement el3 in terms1)
{
//
// print the term name
//
PrintTermName(4, el3);
}
}
}
}
}
 
/// <summary>
///
/// </summary>
/// <param name=”count”></param>
/// <returns></returns>
public static string Indent(int count)
{
return “”.PadLeft(count);
} /// <summary>
/// <summary>
///
/// </summary>
/// <param name=”el”></param>
/// <returns></returns>
private static bool IsParentTerm(XElement el)
{
return (null == el.Element(TERMMOREINFO_ELEMNODE).Element(TERMMOREINFOCHILD_ELEMNODE).Attribute(PARENTTERMID_ATTR));
}
/// <summary>
///
/// </summary>
/// <param name=”el”></param>
private static void PrintTermSetName(XElement el)
{
Console.WriteLine(Indent(0) + el.Attribute(TERMSETNAME_ATTR).Value);
}
/// <summary>
///
/// </summary>
/// <param name=”indent”></param>
/// <param name=”el2″></param>
private static void PrintTermName(int indent, XElement el2)
{
Console.WriteLine(Indent(indent) + GetTermName(el2));
}
/// <summary>
///
/// </summary>
/// <param name=”el2″></param>
/// <returns></returns>
private static string GetTermName(XElement el2)
{
return el2.Element(LABELSET_ELEMNOE).Element(LABELTERM_ELEMNOE).Attribute(LABELNAME_ATTR).Value;
}
#endregion
}
}

About Pankaj

I am a Developer and my linked profile is https://www.linkedin.com/in/pankajsurti/
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s