3

I've got a weird error here I don't quite understand. I have a very simple class with nothing but string properties and a couple of methods. One of those methods is a static function that returns a list of the object. When I attempt to instantiate an XmlSerializer with the type, though, I get an InvalidOperationException with a NullReferenceException inner exception . The declaration of properties and class looks like this:

[Serializable]
public class Config
{
    public string Name { get; set; }       
    public string DatabaseInstanceName { get; set; }
    public string InitialCatalog { get; set; }
    public string PersistSecurityInfo { get; set; } = "true";
    public string UserID { get; set; }
    public string Password { internal get; set; }

    public Config() { } //declared explicitly in case this was the pitfall, but didn't work
    public void Save()....
    public SqlConnection GetConn()...
    public static IList<Config> LoadAllConfigurations...

The static function where the error occurs doesn't get far.

  public static IList<Config> LoadAllConfigurations()
    {
        var t = typeof(Config);
        var xml = new XmlSerializer(t); //error occurs here

I confirmed that t contains the Type Config, so what about my instantiation am I doing incorrectly? As you can see, I added a parameterless constructor explicitly to see if there as a failure here, but nothing changed. Update: I also tried removing the default value for PersistSecurityInfo. InvalidOperationException outer exception says there was a problem reflecting type Config.

2
  • 2
    It's the internal modifier on Password getter that causes it. Commented Dec 17, 2018 at 13:54
  • The issue is the root tag of xml cannot be an array which isn't considered "well formed xml". You are passing IList<Config> which is an array. You need to wrap the xml in a root which is not an array. Commented Dec 17, 2018 at 14:16

2 Answers 2

4

steve16351 is correct that it is the internal modifier on the get that causes this - the library code doesn't anticipate that scenario (which is pretty rare, to be fair).

Not in this case, but sometimes the trick with XmlSerializer is to unwrap all the exceptions, i.e.

    catch (Exception ex)
    {
        while (ex != null)
        {

            Console.WriteLine(ex.Message);
            ex = ex.InnerException;
        }
    }

However, in this case all it says is:

There was an error reflecting type 'Config'.
Object reference not set to an instance of an object.

However, sometimes this approach gives a more useful amount of information about the problem.

But: removing the internal fixes is. If you really really don't want that property to be gettable, then create two models - one that is your domain objects for regular usage, and one which is the serialization types just for use with the serializer. Then map between them adjacent to your serialization code. This approach is the "one stop shop" for fixing all nuances of serializers, since you can use whatever approaches the serializer likes, without impacting your "real" types at all.

Also: you can remove [Serializable] - XmlSerializer doesn't care about it.

Sign up to request clarification or add additional context in comments.

Comments

0

Had the same error with a static property having a private getter like this:

public static string Test { private get; set; }

I could workaround the problem by removing the private. But in my case it made more sense to make XmlSerializer ignore the property like this:

[XmlIgnore]
public static string Test { private get; set; }

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.