The following is a snippet of a much larger C# code. For the sake of convenience, I'm only providing the relevant information.
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.
For example If I chose "Additional 1" the values would be listed as 123, 4, 5, 6, but if we were to change it to "Pt 3" the values would be 123, 7, 8, 9. The DValue cannot change for empty nodes.
So this is where the problem comes in.
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. 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. As a result, every time I reference something with an empty node, it treats the DValue as 0 instead of doing nothing. I tried making a portion of code to read each individual DValue, and then perform the following if statement:
if (element.IsEmpty)
{
DFound = false;
}
else
{
DFound = true;
int.TryParse(element.InnerText, out tempTRES.DVal);
}
But it still treats the empty variables as 0, and as a result every value is treated as "true" 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:
- 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.
- element.InnerXml does nothing, and element.Value fails.
- 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 = "-100", didn't actually do anything.
In summation. How can I make a clear distinction for when an XML node is "0", versus it being empty? 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:
int.TryParse(TRES.SelectSingleNode("AValue").InnerText, out tempTRES.AVal);
int.TryParse(TRES.SelectSingleNode("BValue").InnerText, out tempTRES.BVal);
int.TryParse(TRES.SelectSingleNode("CValue").InnerText, out tempTRES.CVal);
//Note: Every TRES needs an DValue node, even if empty.
if (TRES.SelectSingleNode("DOS/TRES/DValue") == null)
{
XmlElement element = TRES.SelectSingleNode("DValue") 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);
}
<?xml version="1.0" encoding="utf-8"?>
<UNO>
<DOS>
<TRES Name="Pt 1">
<DValue></DValue>
<AValue>1</AValue>
<BValue>2</BValue>
<CValue>3</CValue>
</TRES>
<TRES Name="Pt 2">
<DValue></DValue>
<AValue>4</AValue>
<BValue>5</BValue>
<CValue>6</CValue>
</TRES>
<TRES Name="Pt 3">
<DValue></DValue>
<AValue>7</AValue>
<BValue>8</BValue>
<CValue>9</CValue>
</TRES>
<TRES Name="Additional 1">
<DValue>123</DValue>
<AValue>4</AValue>
<BValue>5</BValue>
<CValue>6</CValue>
</TRES>
<TRES Name="Additional 2">
<DValue>654</DValue>
<AValue>3</AValue>
<BValue>2</BValue>
<CValue>1</CValue>
</TRES>
<TRES Name="Additional 3">
<!--If you can really tell the difference between 0 and an empty variable, this shouldn't be a problem-->
<DValue>0</DValue>
<AValue>0</AValue>
<BValue>0</BValue>
<CValue>0</CValue>
</TRES>
</DOS>
</UNO>