XmlSerializer deserialization UnknownAttribute event not firing for built-in types

54 Views Asked by At

I have an event handler for XmlSerializer's UnknownAttribute event. This fires correctly if I have the following document:

<Document>
    <Test unknown="attribute">
        ...
    </Test>
</Document>
public class Test
{
    ...
}

public class Document
{
    public Test Test;
}

However, if instead there was an unknown attribute on one of the built in types, like string or list of strings, the event handler does not trigger:

<Document>
    <TestList unknown="attribute not triggered">
        ...
    </TestList>
    <StringField unknown="attribute not triggered">random</StringField>
</Document>
public class Document
{
    [XmlArrayItem("Test")]
    public List<string> TestList;
    public string StringField;
}

Any ideas for how to get this to fire the same UnknownAttribute event regardless of the element type it was placed on?

Example code to deserialize this is as follows:

var serializer = new XmlSerializer(typeof(Document));
serializer.UnknownAttribute += HandleUnknownAttribute

using (var reader = new FileStream("filename.xml", FileMode.Open))
{
    serializer.Deserialize(reader);
}

...

public void HandleUnknownAttribute(object sender, XmlAttributeEventArgs e)
{
    // not triggered for TestList or StringField
}
0

There are 0 best solutions below