views:

167

answers:

3

I recently read the IXmlSerializable interface information on MSDN and was a little confused about the whole end tag thing.

As I understand it, if you are implementing IXmlSerializable then you do not need to write the end tag, but you do need to read it? E.g. the following would error if my class A does not read the end tag

<A>
   <i>32</i>
</A>

But what happens if the content is

<A i="32"/>

If I attempt to read the end tag here I get an InvalidOperationException. But if I read on to determine what I'm supposed to be reading is that not going to much up the position of the reader?

I think I'm just getting a little lost, as the code I'm working with gets a bit more complex when I have to deal with IXmlSerializable children (which can be of varying types), and collections of IXmlSerializable elements.

+2  A: 

I'm recalling XmlReader.IsEmptyElement. On the start tag if this is set then you have <a />. If it is not set then you have <a></a>. The latter you read the end tag, the former you don't.

Colin Burnett
A: 

I think this bit about not reading the end tag is for a scenario where your object is embedded in a larger structure:

public class Wrapper {
    public A Wrapped;
}

<Wrapper>
    <Wrapped><i>32</i></Wrapped>
</Wrapper>

I expect you'll need to read <Wrapped> and </Wrapped> but not </Wrapper>.

John Saunders
Reason for the downvote?
John Saunders
I'm guessing someone is trying to say your comment's wrong?? (I didn't down vote it).
Ian
It would be good to know where I'm wrong so I don't repeat it.
John Saunders
It probably got voted down because the problem wasn't with the XML but with the unobvious .IsEmptyElement property.
lavinio
Thanks for telling me why you downvoted. I'd have appreciated you being upfront about it. Your reason was correct. I didn't read carefully enough to realize his question wasn't about what the MSDN doc said, but that, in following the doc, he needed IsEmptyElement. Thank you for taking the time to correct me. I hope next time you'll just do it at the time of the downvote. It's what _I_ do.
John Saunders
John, I don't think lavinio was the one who downvoted based on "It probably got". It wouldn't be "probably" if lavinio did.
Colin Burnett
@Colin: "Probably". I happened to notice at the time, that the downvote happened just before his post. Could have been anyone else, but the timing was interesting.
John Saunders
+1  A: 

Both XmlReader and XPathNavigator implement a property called IsEmptyElement.

From the documentation:

When overridden in a derived class, gets a value indicating whether the current node is an empty element without an end element tag.

So when you see a start-element event, if IsEmptyElement is true you should not look for an end-element event. The start-element event acts as both start and end for that case.

lavinio
Ok thanks for the info. I believe I misunderstood what the IsEmptyElement meant. I assumed attributes on an element meant that it wasn't empty, but believe that's incorrect...
Ian
This answer is of help, thanks. I made the same incorrect assumption as Ian, I think the property is not intuitively named.
Abhijeet Kashnia