Sometimes, a certain XMLElement has a value:
<CAR>value</CAR>
sometimes though it does not in the returend XML:
<CAR/>
When using the XmlReader, I am not sure yet how to handle this situation. For example I have code that reads each element. But what if car has no value coming back at times? Then my DateTime.Parse bombs out so I'm not quite sure hot to essentially handle these situations and skip it with XmlReader:
reader.ReadStartElement("CAR");
// error here since car doesn't have a value this time
_tDate = DateTime.Parse(reader.ReadContentAsString());
EDIT:
if(!reader.IsStartElement())
This doesn't work either. After it sees that it's not a start element, I get an error and not sure how to handle skipping it essentially after the if statement
if(!reader.IsStartElement())
{
reader.ReadStartElement("CAR");
_tDate = DateTime.Parse(reader.ReadContentAsString());
}
reader. ReadEndElement(); <-- ERROR HERE not sure how to move on
EDIT 2:
How can I be getting true when the value is <CAR/>
reader.ReadStartElement("In");
_optedInDate = DateTime.Parse(reader.ReadContentAsString());
_userIsOut = false;
reader.ReadEndElement();
if(reader.IsStartElement("CAR"))
{
reader.ReadStartElement("CAR");
_optedOutDate = DateTime.Parse(reader.ReadContentAsString());
_userIsOptedOut = true;
}
reader.ReadStartElement("COLUMNS");
reader.ReadStartElement("COLUMN");
It's clearly NOT a start element but I get true...
EDIT 3:
tried this:
if((reader.NodeType == XmlNodeType.Element) & (!reader.IsEmptyElement))
{
reader.ReadStartElement("CAR");
_optedOutDate = DateTime.Parse(reader.ReadContentAsString());
_userIsOptedOut = true;
}
reader.ReadStartElement("COLUMNS"); Error Here: Element 'COLUMNS' was not found. Line 12, position 2
The nodetype was "whitespace". I can't figure out how the hell to skip that damn <CAR/>.