簡體   English   中英

XmlSerializer 在 .NET 3.5 和 CF.NET 3.5 之間有所不同

[英]XmlSerializer differs between .NET 3.5 & CF.NET 3.5

我有一個在 CF.NET 和 .NET 下運行的庫,但兩者之間的序列化不同。 結果,在 CF.NET 下生成的 XML 文件在 .NET 下無法讀取,這對我來說是個大問題!

這里的代碼示例:

[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>

如何修改我的程序以生成相同的 XML?

確實,它看起來像是處理根名稱的錯誤。 作為一種解決方法:手動控制根:

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

這應該序列化為

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

在任一平台上。 foobar替換為您的首選名稱。

(就個人而言,我會使用二進制 output;p)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM