如何在xml文件中显示日志文件中的值

时间:2009-09-17 17:18:42

标签: c# xml winforms

我在某个文件夹中有一个xml文件sample.xml:

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

这个xml文件是给用户的,如果任何一个超出限制说8或700它将在日志文件中发送默认值为80(它是窗口服务,我们没有任何UI),否则无论给定的是什么显示 如果它是字符串或字母数字,它将向日志文件发送错误消息。

我需要在try中使用c#编码,catch块用于在日志文件中显示这些内容

这个地方意味着他们以前做的功能意味着这里的值是固定的,没有xml文件的时候我们现在需要使用哪一个(xml文件)。

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

2 个答案:

答案 0 :(得分:0)

使用app.config文件而不是单独的XML文件会更容易。然后,您可以使用内置类来读取值。 Here's an example.

答案 1 :(得分:0)

这是我为此功能所做的代码

试            {                string xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString()+“NetIQ.xml”;                FileInfo checkFile = new FileInfo(xmlFilePath);                if(checkFile.Exists)                {//是的                    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;
   }