tags:

views:

95

answers:

2

I have an xml file sample.xml located in some folder:

<?xml version="1.0"?>
<!-- Please Enter your frequency value between 10sec to 80sec --> 
<Time>
  <Value>80</Value>
</Time>

this xml file is given to user if any one enters beyond the limit say 8, or 700 it wl send the default value as 80 in the log file(it is windowservice we dont have any UI) otherwise whatever the given one need to display if it is string or alphanumeric it wl send error message to log file.

I need a c# coding in try ,catch block for displaying those things in log files

This is the place means the function they are previously doing means here the value is fixed that time no xml files are there which one(xml file) we need to use for now.

public sampleInterface()
{
    // This call is required by the Windows.Forms Component Designer.
    InitializeComponent();
    // 
    // NetIqInterface
    // ## Heading ##
    this.CanHandlePowerEvent = false;
    this.CanPauseAndContinue = true;
    this.CanShutdown = false;

    //
    // Initialize static variables
    //
    etl = new EtlDebug( ProjectInstaller.SERVICE_NAME, "netiq", "NIQTRACES" );

    if (outstandingTimers == null) outstandingTimers = new ArrayOfTimers();

    //
    // Initialize polling timer - set to 80 seconds for now.
    //
    LeafDebug.DebugTraceNormal( "InitializeComponent", "Set polling timer: 60000ms" );
    timer = new System.Timers.Timer( 60000 );
    timer.Elapsed += new ElapsedEventHandler( OnTimer );

    // The method in the Leaf.Resources instantiates the resource
    // manager appropriately to access the resource file.
    resources = Managers.LeafStrings;
}
A: 

It'd be easier to use the app.config file instead of a separate XML file. Then you can use the built-in classes to read the values. Here's an example.

David
then where to put apconfig file in setup files
peter
It would go in the same folder as the service.
David
A: 

This is the code what i did for this functionality

try { string xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "NetIQ.xml"; FileInfo checkFile = new FileInfo(xmlFilePath); if (checkFile.Exists) { // True readVal = ReadValFromXML(xmlFilePath);

               if (!(readVal > 10 && readVal <= 600))
               {
                   LeafDebug.DebugTraceNormal("InitializeComponent", "timer: 60sec");
                   readVal = 60000;
               }
               else
               {

                   this.readVal = this.readVal * 1000;
                   LeafDebug.DebugTraceNormal("Modified interval is accepted.", "Set  timer to: " + (this.readVal / 1000) + ".");
               }
           }
           else
           { // False
               LeafDebug.DebugTraceNormal("InitializeComponent", "Set polling timer: 60sec");
               readVal = 60000;
           }
           PassToTimer(readVal);
       }
       catch (Exception ex)
       {

           LeafDebug.DebugTraceSevere("The Error Occured in  file.", ex.ToString());
           LeafDebug.DebugTraceSevere(" Interval Error.", " interval value is not in correct format /  file not found.");
           LeafDebug.DebugTraceNormal("InitializeComponent", "Set  timer: 60sec");
           readVal = 60000;
           PassToTimer(readVal);
       }


/// <summary>
   /// PassToTimer(int readVal): This function will pass the readVal to Timer
   /// </summary>
   /// <param name="readVal"></param>
   private void PassToTimer(int readVal)
   {
       timer = new System.Timers.Timer(readVal);
       timer.Elapsed += new ElapsedEventHandler(OnTimer);
       // The method in the Leaf.Resources instantiates the resource
       // manager appropriately to access the resource file.
       resources = Managers.LeafStrings;
   }

  /// <summary>
   /// ReadValFromXML(string path) : This Method will Read and returns the Pooling interval Value from NetIQPollingInterval.xml.
   /// </summary>
   /// <param name="path"> path determines the working directory of the application</param>
   /// <returns> Returns Pooling interval Value </returns>
   /// <creator> Created by Faishal </creator>
   /// <Date> 24th Aug '09 </Date>
   /// <ReasonForCreation> User can enter the Pooling Interval time by Modifying the value of file </ReasonForCreation>
   /// <xmlFileLocation> Project Folder </xmlFileLocation>
   private Int32 ReadValFromXML(string path)
   {
       XmlDocument xmlDoc = new XmlDocument();
       xmlDoc.Load(path);
       XmlNode node = xmlDoc.SelectSingleNode("Time");
       Int32 val = Int32.Parse(node.FirstChild.InnerText.ToString());
       return val;
   }
peter
Any one know any optimised code other than this,,or any problem in this code
peter