By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

passing data to a custom action

P: n/a
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("appSetting s")
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!

---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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[i].FirstChild;
while (y < nodelist[i].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
Nov 17 '05 #2

P: n/a
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

"Hans [DiaGraphIT]" <ha********@sshf.no> wrote in message
news:1B**********************************@microsof t.com...
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("appSetting s")
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!

---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)

Nov 17 '05 #3

P: n/a
Your not a bad guesser... :-)

thank you very much.

Hans

"Bob Grommes" wrote:
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

"Hans [DiaGraphIT]" <ha********@sshf.no> wrote in message
news:1B**********************************@microsof t.com...
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("appSetting s")
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!

---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)


Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.