1.xml格式
<?xml version="1.0" encoding="utf-8"?>
<Time>
<Print Hour="9" Minute="0" />
</Time>
2.工程源码
public class PrintTimeControlPoweronAction
{
private struct PrintTime
{
public String hour;
public String Minute;
}
private PrintTime ConvertTimeToPrintTime(DateTime dt)
{
PrintTime pt = new PrintTime();
pt.hour = dt.Hour.ToString();
pt.Minute = dt.Minute.ToString();
return pt;
}
public bool TimeAbove15minutes(DateTime dt)
{
PrintTime now = ConvertTimeToPrintTime(dt);
int timenow = TimeToIntNum(now);
PrintTime justnow = ReadNodeAttr();
int timejustnow = TimeToIntNum(justnow);
int timeoff = timenow - timejustnow;
if (timeoff >= 14)
{
return true;
}
else
{
return false;
}
}
private int TimeToIntNum(PrintTime now)
{
int num0 = Convert.ToInt32(now.hour);
int num1 = Convert.ToInt32(now.Minute);
int result = num0 * 60 + num1;
return result;
}
private PrintTime ReadNodeAttr()
{
PrintTime pt = new PrintTime();
try
{
var doc = new System.Xml.XmlDocument();
doc.Load("PrintTime.xml");
System.Xml.XmlNode root = doc.DocumentElement;
if (root != null)
{
System.Xml.XmlNodeList nodelist = root.SelectNodes("Print");
foreach (System.Xml.XmlNode node in nodelist)
{
try
{
if (node.Name == "Print")
{
pt.hour = node.Attributes.Item(0).Value.ToString();
pt.Minute = node.Attributes.Item(1).Value.ToString();
}
break;
}
catch (Exception fe)
{
}
}
}
}
catch (Exception ex)
{
}
return pt;
}
public void UpdateNode(DateTime dt)
{
PrintTime pt = ConvertTimeToPrintTime(dt);
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load("PrintTime.xml"); //加载xml文件
System.Xml.XmlNodeList nodeList = xmlDoc.SelectSingleNode("Time").ChildNodes;
//遍历所有子节点
foreach (System.Xml.XmlNode xn in nodeList)
{
if (xn.Name == "Print")
{
System.Xml.XmlElement xe = (System.Xml.XmlElement)xn;
xe.SetAttribute("Hour", pt.hour);
xe.SetAttribute("Minute", pt.Minute);
xmlDoc.Save("PrintTime.xml");//保存。
break;
}
}
}
}