There is the following class hierarchy:

public class XmlTypeMapping_init { public XmlTypeMapping tm; public XmlTypeMapping_init() { tm = (new SoapReflectionImporter().ImportTypeMapping(typeof(SOAP_Serializer))); } } public class SOAP_Serializer:XmlTypeMapping_init { private StreamWriter file; private XmlSerializer xml_s; public SOAP_Serializer() : base() { xml_s = new XmlSerializer(tm); } ... } 

When you call the specified constructor of the class SOAP_Serializer ( SOAP_Serializer s = new SOAP_Serializer(); ), the parent constructor is called and on the line

 tm = (new SoapReflectionImporter().ImportTypeMapping(typeof(SOAP_Serializer))); 

an error:

Unable to serialize System.Xml.Serialization.XmlTypeMapping, because it does not have a non-parametric constructor

  • XmlSerializer requires that the type being serialized have a default constructor. The XmlTypeMapping type does not have this. You can try using a different serializer ( DataContractSerializer , JsonSerializer , etc.), but better tell what you want to do. - andreycha
  • This is understandable, but does serialization occur in the line where the error occurs? I want to create my own class using SOAP serialization. An example of using XmlSerializer for serialization in SOAP is here: msdn.microsoft.com/ru-ru/library/vstudio/… - AN90

1 answer 1

SoapReflectionImporter eats all public fields. SOAP_Serializer inherited from XmlTypeMapping_init , which has a public field XmlTypeMapping tm , which tries to serialize. Make it protected (if you want to use it as a successor), or set the [SoapIgnore] attribute

  • Thanks, helped! But I did not understand why the tightening from the point of view of the access modifier had such an effect? SoapReflectionImporter.ImportTypeMapping generates an XML schema element mapping for the specified type of the .NET Framework. And where is the serialization? - AN90
  • @ AN90 apparently, at the time of creating the mapping, public members of the type are checked for the possibility of serialization, and if the check fails, an exception is thrown. If very interesting, you can dig into the code and find this place: referencesource microsoft.com/#System.Xml/System/Xml/… - andreycha