XmlSerializer is different from .NET 3.5 and CF.NET 3.5

I have a library that rotates under CF.NET and .NET, but serialization is different between them. As a result, the XML file created in CF.NET cannot be read in .NET, and for me this is a big problem!

Here is an example code:

[Serializable, XmlRoot("config")] public sealed class RemoteHost : IEquatable<RemoteHost> { // ... } public class Program { public static void Main() { RemoteHost host = new RemoteHost("A"); List<RemoteHost> hosts = new List<RemoteHost>(); hosts.Add(host); XmlSerializer ser = new XmlSerializer(typeof(List<RemoteHost>)); ser.Serialize(Console.Out, hosts); } } 

CF.NET xml:

 <?xml version="1.0"?> <ArrayOfConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <config Name="A"> </config> </ArrayOfConfig> 

.NET xml

 <?xml version="1.0" encoding="ibm850"?> <ArrayOfRemoteHost xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <RemoteHost Name="A"> </RemoteHost> </ArrayOfRemoteHost> 

How can I modify my program to create the same XML?

+6
source share
1 answer

Looks like an error processing the root name. As a workaround: take control of the root manually:

 [XmlRoot("foo")] public class MyRoot { [XmlElement("bar")] public List<RemoteHost> Hosts {get;set;} } 

It should be serialized as

 <foo><bar>...</bar>...</foo> 

on both platforms. Substitute foo and bar for your preferred names.

(I personally would use binary output, however; p)

+4
source

Source: https://habr.com/ru/post/890225/


All Articles