好吧,我在读取 XML 文件时遇到了一些问题。我写的方法似乎只是在一个非常特定的行之后停止(当单步执行它时,它会在执行该行后停止,即使它后面还有更多代码)。
XML:
<?xml version="1.0" encoding="utf-8"?>
<Timers>
<Timer title="Test" x="998" y="197" width="500" height="83" opacity="1" ontop="False">
<Year>2013</Year>
<Day>29</Day>
<Hour>12</Hour>
<Minute>19</Minute>
<Second>25</Second>
<Millisecond>101</Millisecond>
<Note />
</Timer>
</Timers>
方法:
public bool LoadTimers()
{
if (File.Exists(Path.GetDirectoryName(Application.ExecutablePath) + "\\data.pts"))
{
bool foundTimer = false;
XmlDocument doc = new XmlDocument();
doc.Load(Path.GetDirectoryName(Application.ExecutablePath) + "\\data.pts");
foreach (XmlNode node in doc.DocumentElement)
{
if (node.NodeType == XmlNodeType.Element && node.Name == "Timer")
{
//Create a new timer.
Form1 form = new Form1(this);
form.Show();
form.TimerName = node.Attributes["title"].Value.ToString();
form.Left = int.Parse(node.Attributes["x"].Value.ToString());
form.Top = int.Parse(node.Attributes["y"].Value.ToString());
form.Width = int.Parse(node.Attributes["width"].Value.ToString());
form.Height = int.Parse(node.Attributes["height"].Value.ToString());
form.Opacity = double.Parse(node.Attributes["opacity"].Value.ToString());
form.TopMost = bool.Parse(node.Attributes["ontop"].Value.ToString());
//Date.
int year = 0, day = 0, hour = 0, minute = 0, second = 0, millisecond = 0;
string note = "";
foreach (XmlNode dateNode in node.ChildNodes)
{
if (dateNode.NodeType == XmlNodeType.Element)
{
if (dateNode.Name == "Year") year = int.Parse(dateNode.Value);
if (dateNode.Name == "Day") day = int.Parse(dateNode.Value);
if (dateNode.Name == "Hour") hour = int.Parse(dateNode.Value);
if (dateNode.Name == "Minute") minute = int.Parse(dateNode.Value);
if (dateNode.Name == "Second") second = int.Parse(dateNode.Value);
if (dateNode.Name == "Millisecond") millisecond = int.Parse(dateNode.Value);
if (dateNode.Name == "Note") note = dateNode.Value;
}
}
foundTimer = true;
}
}
return foundTimer;
}
return false;
}
它刚刚停止的行是这样的:
if (dateNode.Name == "Year") year = int.Parse(dateNode.Value);
if 语句为真,它执行“year = int.Parse(dateNode.Value);” 但它不会从那里继续。它只是停止执行该方法。