timer+xml

E

Elliot

I am writing a program to get the content of a xml file at a interval time.
However, label1.Text did not change. Can anyone find the problem for me?
Any idea would be appreciated.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
System.Timers.Timer mainxmltmr = new System.Timers.Timer(10000);
mainxmltmr.Elapsed += new
System.Timers.ElapsedEventHandler(mainxmltmr_Elapsed);
mainxmltmr.Start();
}

private void mainxmltmr_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
try
{
string strXmlFile = "http://localhost/xml.xml";
XmlDocument objXml = new XmlDocument();
objXml.Load(strXmlFile);
XmlElement objDocumentRoot = objXml.DocumentElement;

label1.Text = objDocumentRoot.GetAttribute("value");
}
catch (Exception){}
}

}
}


<?xml version="1.0" encoding="UTF-8" ?>
<AF value="hi">
</AF>
 
P

Pavel Minaev

I am writing a program to get the content of a xml file at a interval time..
However, label1.Text did not change. Can anyone find the problem for me?
Any idea would be appreciated.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Timers.Timer mainxmltmr = new System.Timers.Timer(10000);
            mainxmltmr.Elapsed += new
System.Timers.ElapsedEventHandler(mainxmltmr_Elapsed);
            mainxmltmr.Start();
        }

        private void mainxmltmr_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
        {
            try
            {
                string strXmlFile = "http://localhost/xml.xml";
                XmlDocument objXml = new XmlDocument();
                objXml.Load(strXmlFile);
                XmlElement objDocumentRoot = objXml.DocumentElement;

                label1.Text = objDocumentRoot.GetAttribute("value");
            }
            catch (Exception){}
        }

    }

}

<?xml version="1.0" encoding="UTF-8" ?>
<AF value="hi">
</AF>

The obvious idea is that XmlDocument.Load throws an exception; what
really hints at it is that you have encased it into try-catch for
whatever reason. How about removing that catch, and seeing if it
throws anything (and if so, what is it)?
 
E

Elliot

I tried another method and no problem found.
Thanks for your idea.


Paul E Collins said:
Elliot said:
I am writing a program to get the content of a xml file at a interval
time.
However, label1.Text did not change. Can anyone find the problem for me?
Any idea would be appreciated.
[...]
catch (Exception){}

Handle the exception, then.

Eq.
 
E

Elliot

Great! I will try it. Thanks.



Peter Duniho said:
The exception is almost certainly the Control class's cross-thread
exception, seeing as he's trying to set the Text property from within the
event handler for a System.Timers.Timer object.

To the OP: you either need to switch to the System.Windows.Forms.Timer
class or use Control.Invoke() to assign the Text property.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top