1

Alright I'm having some problems reading an XML file. The method I wrote seems to just stop after a very specific line (when stepping through it just stops after it has executed that line even though there's more code after it).

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>

Method:

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;
        }

The line it just stops at is this:

if (dateNode.Name == "Year") year = int.Parse(dateNode.Value);

The if statement is true and it does execute "year = int.Parse(dateNode.Value);" but it doesn't continue from there. It just stops executing the method.

1 Answers1

0

I tried your code and yes, it throws an ArgumentNullException because dateNode does not have a Value. The data you are trying to read are stored in properties InnerText and InnerXml.

Replace everywhere

int.Parse(dateNode.Value);

for

int.Parse(dateNode.InnerText);
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • Oh wauw that worked! Can you tell me why using .Value makes the method stop? –  Aug 29 '13 at 10:47
  • @Spiderhund: you probably run this code within a try-catch block and it consumes the exception silently. If you have a VS version with IntelliTrace , you can see all exceptions thrown. Otherwise, make sure to read console output. – pbalaga Aug 29 '13 at 10:49
  • Yes, because .Value is null. It contains no text, it points to nowhere and you can't basically read from nowhere :) It would be useful, if you read the article I posted for you. There you can learn about debugging your code (finding out what is wrong). – Ondrej Janacek Aug 29 '13 at 10:49
  • Ahh I see :) I'll read through the article, I just got confused because I've never run into this problem before :P Anyways thank you! –  Aug 29 '13 at 10:52
  • Also, in general, things failing silently may occur when you don't keep a strong reference to an object and it gets garbage-collected. See: http://stackoverflow.com/q/15517877/318795 . Just saying for the sake of completeness, although it's not the case here. – pbalaga Aug 29 '13 at 11:03