I’m trying to add XML serialization to a fairly trivial class structure in C#. Essentially, there’s a single instance of a root class (call it AClass), which holds a List of several instances of some other class (call it AnotherClass):
[XmlRoot("RootNode")]
public class AClass {
[XmlElement("ListNode")]
internal List otherObjects { get; set; }
}
public class AnotherClass {
[XmlAttribute("Name")]
internal string name { get; set; }
}
When serializing, I’d like for both these classes to be serialized together – that is, if I serialize AClass, its list of AnotherClass gets serialized as well (see this question).
I have this mostly working, but the problem is that during serialization, XmlSerializer only seems to want to deal with public properties of the class – it doesn’t serialize AnotherClass at all if the list is declared internal.
I tried making the assembly’s internals visible to the serializer:
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Xml")]
That didn’t seem to do anything. Is there a way I can get XmlSerializer to recursively serialize lists of objects that are declared internal?
You’re on the right track… except that the actual serialization is not performed by System.Xml, but by a dynamically generated assembly. You can’t predict the name of that assembly (it’s randomly generated), so you can’t use it in the
InternalsVisibleToattribute.The only solution is to pre-generate the XML serialization assembly. You can do that using the XML Serializer Generator Tool (Sgen.exe). The name of the generated assembly will be “YourAssembly.XmlSerializers” ; that’s the name you have to use in the
InternalsVisibleToattribute.