1

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

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.