passing data to a custom action

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hans [DiaGraphIT]

    #1

    passing data to a custom action

    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.XmlN ode
    Dim FoundIt As Boolean = False
    For Each Node In XmlDocument.Ite m("configuratio n").Item("appSe ttings")
    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#)
  • Hans [DiaGraphIT]

    #2
    RE: passing data to a custom action


    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.Collect ions.IDictionar y stateSaver)
    {
    //Get the parameter passed across in the CustomActionDat a
    string strServerName = this.Context.Pa rameters["SNAME"];
    string strDatabaseName = this.Context.Pa rameters["DBNAME"];

    if(strServerNam e == "" || strDatabaseName == "")
    throw new InstallExceptio n ("No arguments specified");

    //Use reflection to find the location of the config file
    Assembly Asm = Assembly.GetExe cutingAssembly( );
    FileInfo fileInfo = new FileInfo(Asm.Lo cation + ".config");

    if (!fileInfo.Exis ts)
    throw new InstallExceptio n ("Missing Config file");


    //Load the config file into the XML DOM.
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Loa d (fileInfo.FullN ame);

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

    bool foundServerName = false;
    bool foundDataBaseNa me = false;

    for (int i = 0; i < nodelist.Count; i++)
    {
    int y = 0;
    XmlNode n = nodelist[i].FirstChild;
    while (y < nodelist[i].ChildNodes.Cou nt)
    {
    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 ;
    foundDataBaseNa me = true;
    }
    }
    n = n.NextSibling;
    y++;
    }
    }

    if(!foundDataBa seName || !foundServerNam e)
    throw new InstallExceptio n ("config file did not contain a servername or a
    databasename section");

    //Write out the new config file.
    xmlDocument.Sav e (fileInfo.FullN ame);
    }


    best regards

    Hans

    Comment

    • Bob Grommes

      #3
      Re: passing data to a custom action

      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["configurat ion"]["appSetting s"])

      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]" <hans.arsjo@ssh f.no> wrote in message
      news:1B352A71-25EA-41E4-A8BA-0B67DCCA145C@mi crosoft.com...[color=blue]
      > 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.XmlN ode
      > Dim FoundIt As Boolean = False
      > For Each Node In XmlDocument.Ite m("configuratio n").Item("appSe ttings")
      > 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#)[/color]


      Comment

      • Hans [DiaGraphIT]

        #4
        Re: passing data to a custom action

        Your not a bad guesser... :-)

        thank you very much.

        Hans





        "Bob Grommes" wrote:
        [color=blue]
        > 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["configurat ion"]["appSetting s"])
        >
        > 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]" <hans.arsjo@ssh f.no> wrote in message
        > news:1B352A71-25EA-41E4-A8BA-0B67DCCA145C@mi crosoft.com...[color=green]
        > > 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.XmlN ode
        > > Dim FoundIt As Boolean = False
        > > For Each Node In XmlDocument.Ite m("configuratio n").Item("appSe ttings")
        > > 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#)[/color]
        >
        >
        >[/color]

        Comment

        Working...