title: Reading XML nodes in a C# program without the use of int.TryParse(node.InnerText)
body: <p>The following is a snippet of a much larger C# code. For the sake of convenience, I'm only providing the relevant information.</p>
  <p>I'm trying to do a program that reads XML variables and puts them in a list. You then choose a name from the list based off of the names listed in the XML, and the values for that specific name are then shown in a series of text boxes in a Form. DValue is special, because it is a value that doesn't have to exist, so because of this, several elements in the XML form have empty nodes for DValue. An empty node should not affect the DValue previously presented.</p>
  <p>For example If I chose &quot;Additional 1&quot; the values would be listed as 123, 4, 5, 6, but if we were to change it to &quot;Pt 3&quot; the values would be 123, 7, 8, 9. The DValue cannot change for empty nodes.</p>
  <p>So this is where the problem comes in.</p>
  <p>I was using  int.TryParse(node.InnerText) to read my XML values, convert them into integers and display them in the form's boxes, but I recently found out that  int.TryParse(node.InnerText) reads every node with said name, which makes things very messy. <strong>Basically, all of my empty nodes are being turned into 0 due to int.TryParse having 0 as a default value when it fails, and because of that, the program will not recognize when there's an empty node</strong>. As a result, every time I reference something with an empty node, <strong>it treats the DValue as 0 instead of doing nothing</strong>. I tried making a portion of code to read each individual DValue, and then perform the following if statement:</p>
  <pre><code>if (element.IsEmpty)
  {
  DFound = false;
  }
  else
  {
  DFound = true;
  int.TryParse(element.InnerText, out tempTRES.DVal);
  }
  </code></pre>
  <p>But it still treats the empty variables as 0, and as a result every value is treated as &quot;true&quot; for the variable DFound. I've been working through a bunch of different potential solutions, but none of them seem to have worked because the empty variables would either be turned to zero, or the entire series of nodes just wouldn't load at all, but I'll leave that aside since this is already a pretty long question, and honestly, I'd have a tough time explaining some of the things I had done, but here are a few notable statements at least:</p>
  <ul>
  <li>string DValueText = Convert.ToString(element): I tried to convert a string value of each node into an integer so I'd never have to use InnerText, but trying to read nodes just breaks the code for reason's I'm not completely aware of yet.</li>
  <li>element.InnerXml does nothing, and element.Value fails.</li>
  <li>I tried setting the InnerText to something else, that way I could at least turn the empty variable into something that I could single out in an if statement, but the method I tried element.InnerText = &quot;-100&quot;, didn't actually do anything.</li>
  </ul>
  <p>In summation. <strong>How can I make a clear distinction for when an XML node is &quot;0&quot;, versus it being empty?</strong> Also, I'm sorry if this seems like a very simple problem. I've looked into this, but solutions for adding node values seem to always revolve around the assumption that one would be using int.TryParse(node.InnerText). Lastly, if something about this doesn't make sense, please let me know. As said, this is just a part of a much larger code, so it's very possible that some contextual information is missing.
  The code and the XML can be found below:</p>
  <hr />
  <pre><code>int.TryParse(TRES.SelectSingleNode(&quot;AValue&quot;).InnerText, out tempTRES.AVal);
  int.TryParse(TRES.SelectSingleNode(&quot;BValue&quot;).InnerText, out tempTRES.BVal);
  int.TryParse(TRES.SelectSingleNode(&quot;CValue&quot;).InnerText, out tempTRES.CVal);
  //Note: Every TRES needs an DValue node, even if empty.
  if (TRES.SelectSingleNode(&quot;DOS/TRES/DValue&quot;) == null)
  {
  XmlElement element = TRES.SelectSingleNode(&quot;DValue&quot;) as XmlElement;
  if (element.IsEmpty)
  {
  DFound = false;
  }
  else
  {
  DFound = true;
  int.TryParse(element.InnerText, out tempTRES.DVal);
  }
  
  }
  viewTRES_.Add(tempTRES);
  viewTRES_.Add(tempTRES);
  cbDVizViewTRES.Items.Add(tempTRES.name);
  }
  </code></pre>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;UNO&gt;
  &lt;DOS&gt;
  &lt;TRES Name=&quot;Pt 1&quot;&gt;
  &lt;DValue&gt;&lt;/DValue&gt;
  &lt;AValue&gt;1&lt;/AValue&gt;
  &lt;BValue&gt;2&lt;/BValue&gt;
  &lt;CValue&gt;3&lt;/CValue&gt;
  &lt;/TRES&gt;
  &lt;TRES Name=&quot;Pt 2&quot;&gt;
  &lt;DValue&gt;&lt;/DValue&gt;
  &lt;AValue&gt;4&lt;/AValue&gt;
  &lt;BValue&gt;5&lt;/BValue&gt;
  &lt;CValue&gt;6&lt;/CValue&gt;
  &lt;/TRES&gt;
  &lt;TRES Name=&quot;Pt 3&quot;&gt;
  &lt;DValue&gt;&lt;/DValue&gt;
  &lt;AValue&gt;7&lt;/AValue&gt;
  &lt;BValue&gt;8&lt;/BValue&gt;
  &lt;CValue&gt;9&lt;/CValue&gt;
  &lt;/TRES&gt;
  &lt;TRES Name=&quot;Additional 1&quot;&gt;
  &lt;DValue&gt;123&lt;/DValue&gt;
  &lt;AValue&gt;4&lt;/AValue&gt;
  &lt;BValue&gt;5&lt;/BValue&gt;
  &lt;CValue&gt;6&lt;/CValue&gt;
  &lt;/TRES&gt;
  &lt;TRES Name=&quot;Additional 2&quot;&gt;
  &lt;DValue&gt;654&lt;/DValue&gt;
  &lt;AValue&gt;3&lt;/AValue&gt;
  &lt;BValue&gt;2&lt;/BValue&gt;
  &lt;CValue&gt;1&lt;/CValue&gt;
  &lt;/TRES&gt;
  &lt;TRES Name=&quot;Additional 3&quot;&gt;
  &lt;!--If you can really tell the difference between 0 and an empty variable, this shouldn't be a problem--&gt;
  &lt;DValue&gt;0&lt;/DValue&gt;
  &lt;AValue&gt;0&lt;/AValue&gt;
  &lt;BValue&gt;0&lt;/BValue&gt;
  &lt;CValue&gt;0&lt;/CValue&gt;
  &lt;/TRES&gt;
  &lt;/DOS&gt;
  &lt;/UNO&gt;
  </code></pre>