如何从 xml 文件显示日志文件中的值 [英] how to display Value in log file from an xml file

查看:22
本文介绍了如何从 xml 文件显示日志文件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

如果任何人输入超过限制,例如 8 或 700,则将此 xml 文件提供给用户,它将在日志文件中发送默认值作为 80(它是 windowservice,我们没有任何 UI),否则无论给定的人需要什么显示如果它是字符串或字母数字,它会向日志文件发送错误消息.

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.

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

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

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

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

推荐答案

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

This is the code what i did for this functionality

试试{字符串 xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "NetIQ.xml";FileInfo checkFile = new FileInfo(xmlFilePath);如果(检查文件.存在){//真的readVal = ReadValFromXML(xmlFilePath);

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

这篇关于如何从 xml 文件显示日志文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆