【问题标题】:how to display Value in log file from an xml file如何从 xml 文件中显示日志文件中的值
【发布时间】:2009-09-17 17:18:41
【问题描述】:

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

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

如果任何人输入超出限制,例如 8 或 700,则此 xml 文件将提供给用户显示 如果它是字符串或字母数字,它将向日志文件发送错误消息。

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

这是这个地方意味着他们之前正在执行的功能意味着这里的值是固定的,那时没有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;
}

【问题讨论】:

  • 我上面给出的代码是旧代码。问题是这里所有的东西都硬编码了频率,但是这里我们需要开发一个用户驱动的代码,,,意味着我们需要给一个xml文件用户的值设置在一个限制之间,这就是我们需要的

标签: c# xml winforms


【解决方案1】:

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

【讨论】:

  • 然后将 apconfig 文件放在安装文件中的什么位置
  • 它将与服务放在同一个文件夹中。
【解决方案2】:

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

试试 { 字符串 xmlFilePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "NetIQ.xml"; FileInfo checkFile = new FileInfo(xmlFilePath); 如果(检查文件。存在) { // 真的 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;
   }

【讨论】:

  • 任何人都知道除此之外的任何优化代码,或此代码中的任何问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
相关资源
最近更新 更多