GhostHunterJim
GhostHunterJim

Reputation: 89

How can I convert this InstallScipt code to C#?

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;
        }

Upvotes: 0

Views: 189

Answers (2)

Michael Edenfield
Michael Edenfield

Reputation: 28338

The .NET Framework's System.Xml.XmlDocument object gives you the same behavior as the Microsoft.XMLDOM COM objects, as described in this MSDN article.

In this case, your code would look something like this:

var xml = new XmlDocument();
xml.PreserveWhiteSpace = true;
xml.Load(szCryptomaticConfigFile);

string szXPath = CRYPTOMATIC_SETTINGS_PATH;
var settingsNode = xml.selectSingleNode(szXPath);

string szXValue = CRYPTOMATIC_SETTINGS_VALUE;
settingsNode.InnerText = szXValue;

xml.Save(zzCryptomaticConfigFile);

One thing to note is that many of the load/save options are gone. These are no longer set on the top-level XmlDocument. Rather, those are settings on the XmlTextReader that is used to save and load the document. By default, the Load() method is synchronous and validating. Normally this is what you want.

However, if you XML contains DTD references that are off-site hosted and it takes a long time to resolve those DTD references. This is usually why validateOnParse gets turned off. If you need to do this, you need to use an overloaded Load method with your own reader. You can turn off the bit that resolves DTD references like this:

var settings = new XmlReaderSettings();
settings.XmlResolver = null;
settings.ProhibitDtd = false;

var reader = XmlTextReader.Create(szCryptomaticConfigFile, settings);
xml.Load(reader)

Upvotes: 0

Jamiec
Jamiec

Reputation: 136074

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);

Upvotes: 1

Related Questions