我有如下 XML 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<sami>
<title>IN[1]=false</title>
<title>IN[2]=true</title>
<title>OUT[1]=true</title>
<title>OUT[2]=flase</title>
<title>OUT[3]=flase</title>
<title>OUT[4]=flase</title>
<title>$IN[5]=false</title>
</sami>
问题:如何使用 C# 每秒读取 xml 数据?
我尝试了以下方法:
private void Form1_Load(object sender, EventArgs e)
{
DateTime nextRead;
Thread thread = new Thread(() =>
{
nextRead = DateTime.Now.AddSeconds(1000);
XDocument doc = XDocument.Load("C:\\Users\\lenovo\\Desktop\\Sxml.xml");
var result = doc.Descendants("title").ToList();
textBox1.Text = result[0].Value;
// listBox1.Items.Add(result[0].Value);
// listBox1.Items.Add(result[1].Value);
// listBox1.Items.Add(result[2].Value);
Thread.Sleep(Math.Max(0, (DateTime.Now - nextRead).Milliseconds));
});
thread.Start();
}