user72603
user72603

Reputation: 1007

How to handle When XML Element does not have a value

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/>.

Upvotes: 1

Views: 5009

Answers (3)

Alex
Alex

Reputation: 7858

Took me a while to figure out as well. You should do it like this:

if (!reader.IsEmptyElement)
{
    reader.ReadStartElement("CAR");

    // Read the content
    _tDate = DateTime.Parse(reader.ReadContentAsString());

    reader.ReadEndElement();
}
else
    reader.Skip();

That's with XmlReaderSettings's IgnoreWhitespace = true, otherwise you have to check for a whitespace node, too.

Upvotes: 0

Ian
Ian

Reputation: 34549

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

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1504062

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.

Upvotes: 1

Related Questions