0

This is my first attempt at serializing an XML and I need to understand why errors occur in my code:

private void function(Object2 InputParameters)
{

   XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
   ns.Add("", "");
   XmlSerializer s = new XmlSerializer(typeof(Object1));
   StringWriter XMLWriter = new StringWriter();
   s.Serialize(XMLWriter, InputParameters, ns);

   XmlDocument DOC_Xml = new XmlDocument();
   DOC_Xml.LoadXml(XMLWriter.ToString());
}

InnerException:

{"An object of type 'SRV.Entities.Object2' cannot be converted to type 'SRV.Entities.Object1'."}

StackTrace:

"   in System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)\r\n   in System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)\r\n "

The error line is at s.Serialize(XMLWriter, ParametrosEntrada, ns); but I don't understand the reason. How could I solve the serialization between different objects? Thank you, guys.

dbc
  • 104,963
  • 20
  • 228
  • 340
cassmg
  • 1
  • Xml serialization requires the serialize class and the deserialize class to be the same with some exceptions 1) Only the Public variables will be looked at 2) The object types has to be the same. So you can't have a string in a deserialize class while the xml tag contains children. – jdweng Jun 03 '20 at 15:58
  • Can you please share a [mcve] that demonstrates the problem? Probably your `InputParameters` object is not of type `Object1` but we need to confirm. And if so, it's not clear from your question whether you are trying to serialize a polymorphic type hierarchy where `Object2` is a subclass of `Object1`. See [ask]: *Help others reproduce the problem... if your problem is with code you've written, you should include some... Include just enough code to allow others to reproduce the problem.* – dbc Jun 03 '20 at 18:17
  • If you just need to convert an object to an XML string, you can use [this answer](https://stackoverflow.com/a/11448270/3744182) to [Convert an object to an XML string](https://stackoverflow.com/q/11447529/3744182). However, it should be more performant to serialize directly to an `XmlDocument` without the intermediate `string` representation. To do that use `XmlNodeExtensions.AsXmlDocument()` from [this answer](https://stackoverflow.com/a/42123127/3744182) to [Populating ANY elements on a web service in C#](https://stackoverflow.com/q/42114531/3744182). – dbc Jun 03 '20 at 18:27

1 Answers1

1

You have a bug

private void function(Object2 InputParameters)

and

XmlSerializer s = new XmlSerializer(typeof(Object1));

you need

XmlSerializer s = new XmlSerializer(typeof(Object2));

aka, you need to consistently have "Object2" as the input parameter AND the argument of "typeof".

More deeply, you can consider Generics. So you can pass the type is.... as needed . Either Object1 OR Object2. (but not both)

private void MyFunction<T>(T InputParameters)
{

   XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
   ns.Add("", "");
   XmlSerializer s = new XmlSerializer(typeof(T));
   StringWriter XMLWriter = new StringWriter();
   s.Serialize(XMLWriter, InputParameters, ns);

   XmlDocument DOC_Xml = new XmlDocument();
   DOC_Xml.LoadXml(XMLWriter.ToString());
}

You can see more at this SOF answer:

Using generics with XmlSerializer

granadaCoder
  • 26,328
  • 10
  • 113
  • 146