2009-09-17 11 views
0

J'ai un fichier xml sample.xml situé dans certains dossier:comment afficher la valeur dans le fichier journal à partir d'un fichier xml

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

ce fichier xml est donnée à l'utilisateur si quelqu'un entre au-delà de la limite dire 8 , ou 700 il wl envoyer la valeur par défaut 80 dans le fichier journal (il est windowservice nous n'avons pas d'interface utilisateur) sinon quel que celui donné doit afficher s'il s'agit d'une chaîne ou alphanumérique il wl envoyer un message d'erreur au fichier journal.

J'ai besoin aC# codage essayer, bloc catch pour afficher ces choses dans des fichiers journaux

C'est l'endroit signifie que la fonction qu'ils font déjà des moyens ici la valeur est fixe ce moment aucun fichier xml sont là qui un (fichier xml) que nous devons utiliser pour le moment.

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

Le code ce que je ci-dessus est une vieille chose code.The est ici tout hardcoded la fréquence ,, mais ici nous avons besoin d'élaborer un code de userdriven ,,, signifie que nous devons donner un fichier XML à l'utilisateur où les valeurs sont définies entre une limite, c'est ce dont nous avions besoin – peter

Répondre

0

Ce code est ce que je l'ai fait pour cette fonctionnalité

essayer { chaîne xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "NetIQ.xml"; FileInfo checkFile = nouveau FileInfo (xmlFilePath); if (checkFile.Exists) {// Vrai 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; 
    } 
+0

Tout le monde connaît un code optimisé autre que ceci, ou tout problème dans ce code – peter

0

Il serait plus facile d'utiliser le fichier app.config au lieu d'un fichier XML séparé. Vous pouvez ensuite utiliser les classes intégrées pour lire les valeurs. Here's an example.

+0

puis où placer le fichier apconfig dans les fichiers d'installation – peter

+0

Il irait dans le même dossier que le service. – David

Questions connexes