Thursday, November 13, 2008

Get nAnt Build File Property Attribute Programatically

In order to read nAnt build file and get the Property value I wrote the following code in C#.NET

static String GetNANTAttributeValue(String filePath, String tagName, String attribute1Name, String attribute1Value, String attribute2Name)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(filePath);
XmlNodeList xmlNodeList = xmlDocument.GetElementsByTagName(tagName);
String result = "";
for (int i = 0; i < xmlNodeList.Count; ++i)
{
if (xmlNodeList[i].Attributes[attribute1Name].Value == attribute1Value)
{
result = xmlNodeList[i].Attributes[attribute2Name].Value;
break;
}
}
return result;
}
catch (Exception)
{
throw new Exception("Unable to Retrive XML Attribute, The tagName Provided: " +tagName+ " ");
}
}

No comments: