Bluehill.Analyzers

BH0004, BH0005, BH0006: Rules related to IXmlSerializable.GetSchema()

The following rules have a similar purpose: do not use IXmlSerializable.GetSchema(). This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null from this method, and instead, if specifying a custom schema is required, apply the XmlSchemaProviderAttribute to the class.

BH0004: IXmlSerializable.GetSchema() must be explicitly implemented

IXmlSerializable.GetSchema() must be implemented explicitly.

BH0005: IXmlSerializable.GetSchema() must return null

IXmlSerializable.GetSchema() must return null, and must not be abstract or throw exceptions.

BH0006: Never call IXmlSerializable.GetSchema()

IXmlSerializable.GetSchema() should never be called.

Code with violation

public class Class1 : IXmlSerializable {
    public XmlSchema? GetSchema() => new();
    public void ReadXml(XmlReader reader) => throw new NotImplementedException();
    public void WriteXml(XmlWriter writer) => throw new NotImplementedException();

    public XmlSchema? Test() => GetSchema();
}

Fixed Code

public class Class1 : IXmlSerializable {
    XmlSchema? IXmlSerializable.GetSchema() => null;
    public void ReadXml(XmlReader reader) => throw new NotImplementedException();
    public void WriteXml(XmlWriter writer) => throw new NotImplementedException();
}