tags:

views:

342

answers:

7

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());
A: 

Read the content first, then either call DateTime.Parse or not depending on whether there's content. If you get "bad" content sometimes which you want to skip, you should look at DateTime.TryParse too. As to what exactly you should do in the grander scheme of things if you get an empty element, that entirely depends on your application - we can't really tell you that.

Jon Skeet
I tried this but for some reason IsEmptyElement returns false even though I the response is returning a <CAR/> if(!reader.IsEmptyElement) { do my thing }
I was asking how to check for an empty element basically and this is the way but oddly I'm getting false for that element saying it has a value when it does not
Do you actually have <CAR /> or <CAR></CAR>? They're not the same thing.
Jon Skeet
here we go if(!reader.IsStartElement())
That's not really ideal. If you could post a short but complete piece of code showing the problem, along with some sample XML, that would help.
Jon Skeet
A: 

if(!reader.IsStartElement())

+1  A: 

If the reader you are using is an XMLReader then use:

XmlReader reader;
reader.IsEmptyElement // to determine if you should try and parse the value.

Be aware that you might want something alone the lines of the following to ensure you're reading content...

Reader.Read();
Reader.MoveToContent();
Ian
A: 

SOB, 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
A: 

sh** how can I be getting true when the value is

           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...wtf
A: 

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/>
A: 

got it working with the MoveToContent. Thanks.