I'm converting my InstallScript Custom Actions To Managed Custom Actions using the DTF (Microsoft Deployment Foundation) Namespace. There is a piece of InstallScript code that I'm having trouble with converting to C# where I need to do some XML File manipulation. The original InstallScript Code is below. My code below it is how I've converted it so far. Is there a better way of converting it so that I can take advantage of the dot notation (Intellisense) instead of having the XML document object latebound.
set oDoc = CoCreateObject("Microsoft.XMLDOM");
if (IsObject(oDoc)) then
oDoc.async = FALSE;
oDoc.validateOnParse = FALSE;
oDoc.resolveExternals = FALSE;
oDoc.preserveWhiteSpace = VARIANT_TRUE;
oDoc.load(szCryptomaticConfigFile);
szXPath = CRYPTOMATIC_SETTINGS_PATH;
set oSettingsNode = oDoc.selectSingleNode(szXPath);
szValue = CRYPTOMATIC_SETTINGS_VALUE;
oSettingsNode.nodeTypedValue = szValue;
oDoc.Save(szCryptomaticConfigFile);
endif;
My Conversion
dynamic oXMLDOMDoc = Activator.CreateInstance(Type.GetTypeFromProgID("Microsoft.XMLDOM"));
if (oXMLDOMDoc != null)
{
oXMLDOMDoc.async = false;
oXMLDOMDoc.validateOnParse = false;
oXMLDOMDoc.preserveWhiteSpace = VARIANT_TRUE;
oXMLDOMDoc.load(szCryptomaticConfigFile);
string szXPath = CRYPTOMATIC_SETTINGS_PATH;
dynamic oSettingsNode = oXMLDOMDoc.selectSingleNode(szXPath);
string szXValue = CRYPTOMATIC_SETTINGS_VALUE;
oSettingsNode.nodeTypedValue = szXValue;
oXMLDOMDoc.Save(szCryptomaticConfigFile);
return ActionResult.Success;
}
else
{
return ActionResult.Failure;
}
Yes, this is simply opening an XmlDocument
, finding a specific node and updating its value before saving the file again.
The code is along the lines of (untested, typed from memory)
var xml = new XmlDocument();
xml.Load(szCryptomaticConfigFile);
var node = xml.SelectSingleNode(szXPath);
node.Value = CRYPTOMATIC_SETTINGS_VALUE
xml.Save(szCryptomaticConfigFile);