passing data to a custom action

G

Guest

Hi!

Can anyone help me with translating a VB-code to C#?

I'm reading the walkthrough: Passing data to a custom action.
In part 4 were you create an installer class there is a example of VB-code.

I've tried to translate the code to c# but I have problem with a small part
of it ...


' Finds the right node and change it to the new value.
Dim Node As System.Xml.XmlNode
Dim FoundIt As Boolean = False
For Each Node In XmlDocument.Item("configuration").Item("appSettings")
If Node.Name = "add" Then ' skip any comments
If Node.Attributes.GetNamedItem("key").Value = "ServerName" Then
Node.Attributes.GetNamedItem("value").Value = ProvidedName
FoundIt = True
End If
End If
Next Node


please help!
 
G

Guest

while waiting for help I've tried once more...

here is my example of the code.

any comments on the code appreciates.

public override void Install (System.Collections.IDictionary stateSaver)
{
//Get the parameter passed across in the CustomActionData
string strServerName = this.Context.Parameters["SNAME"];
string strDatabaseName = this.Context.Parameters["DBNAME"];

if(strServerName == "" || strDatabaseName == "")
throw new InstallException ("No arguments specified");

//Use reflection to find the location of the config file
Assembly Asm = Assembly.GetExecutingAssembly();
FileInfo fileInfo = new FileInfo(Asm.Location + ".config");

if (!fileInfo.Exists)
throw new InstallException ("Missing Config file");


//Load the config file into the XML DOM.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load (fileInfo.FullName);

//Finds the right node and change it to the new value
XmlNodeList nodelist = xmlDocument.GetElementsByTagName ("appSettings");

bool foundServerName = false;
bool foundDataBaseName = false;

for (int i = 0; i < nodelist.Count; i++)
{
int y = 0;
XmlNode n = nodelist.FirstChild;
while (y < nodelist.ChildNodes.Count)
{
if(n.Name == "add")
{
if (n.Attributes["key"].Value == "SNAME")
{
n.Attributes["value"].Value = strServerName;
foundServerName = true;
}
else if (n.Attributes["key"].Value == "DBNAME")
{
n.Attributes["value"].Value = strDatabaseName;
foundDataBaseName = true;
}
}
n = n.NextSibling;
y++;
}
}

if(!foundDataBaseName || !foundServerName)
throw new InstallException ("config file did not contain a servername or a
databasename section");

//Write out the new config file.
xmlDocument.Save (fileInfo.FullName);
}


best regards

Hans
 
B

Bob Grommes

Hans,

It's always helpful if you explain exactly what the problem is. What error
you are getting on what line.

I'm guessing you're having problem with the Item properties. Usually those
become indexers in C#, so offhand:

foreach (XmlNode node in xmlDocument["configuration"]["appSettings"])

Double check the docs to make sure. You'll see that the Item property of
the XmlDocument has the notation, "In C#, this property is the indexer for
the XmlDocument class".

Also watch out for case sensitivity issues.

That's the best guess I can give you.

--Bob
 
G

Guest

Your not a bad guesser... :)

thank you very much.

Hans





Bob Grommes said:
Hans,

It's always helpful if you explain exactly what the problem is. What error
you are getting on what line.

I'm guessing you're having problem with the Item properties. Usually those
become indexers in C#, so offhand:

foreach (XmlNode node in xmlDocument["configuration"]["appSettings"])

Double check the docs to make sure. You'll see that the Item property of
the XmlDocument has the notation, "In C#, this property is the indexer for
the XmlDocument class".

Also watch out for case sensitivity issues.

That's the best guess I can give you.

--Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top