Unknown elements are just ignored during deserialization

前端 未结 2 1269
走了就别回头了
走了就别回头了 2021-01-07 04:19

When I deserialize an XML document with XmlTextReader, a textual element for which there is no corresponding class is simply ignored.

Note: this is

相关标签:
2条回答
  • 2021-01-07 04:53

    So is this normal and if yes, why?

    Because maybe you're consuming someone else's XML document and whilst they define 300 different elements within their XML, you only care about two. Should you be forced to create classes for all of their elements and deserialize all of them just to be able to access the two you care about?

    Or perhaps you're working with a system that is going to be in flux over time. You're writing code that consumes today's XML and if new elements/attributes are introduced later, they shouldn't stop your tested and deployed code from being able to continue to consume those parts of the XML that they do understand (Insert caveat here that, hopefully, if you're in such a situation, you/the XML author don't introduce elements later which it is critical to understand to cope with the document correctly).

    These are two sides of the same coin of why it can be desirable for the system not to blow up if it encounters unexpected parts within the XML document it's being asked to deserialize.

    0 讨论(0)
  • 2021-01-07 05:02

    As I have found out by accident during further research, this problem is actually ridiculously easy to solve because...

    ...XmlSerializer supports events! All one has to do is to define an event handler for missing elements

    void Serializer_UnknownElement(object sender, XmlElementEventArgs e)
    {
        throw new Exception("Unknown element "+e.Element.Name+" found in "
            +e.ObjectBeingDeserialized.ToString()+" in line "
            +e.LineNumber+" at position "+e.LinePosition);
    }
    

    and register the event with XmlSerializer:

    xmlSerializer.UnknownElement += Serializer_UnknownElement;
    

    The topic is treated at MSDN, where one also learns that

    By default, after calling the Deserialize method, the XmlSerializer ignores XML attributes of unknown types.

    Not surprisingly, there are also events for missing attributes, nodes and objects.

    0 讨论(0)
提交回复
热议问题