1

XML是由使用XMLSerialize序列化/反序列化的c#桌面應用程序通過相同的.NET生成和加載的。反序列化工作正常,但缺少XMLArray c#XMLSerialize

可序列化的類結構非常複雜,所以我只是選擇了兩個相關的類。

現在,當我反序列化,一切都被裝載除了映射消息(或消息的對象列表是如何被調用的組織。

沒有人有這種行爲的解釋?

任何提示。或暗示的改善已經做出什麼也總是讚賞

謝謝

我有以下XML:

<?xml version="1.0" encoding="utf-8"?> 
<configuration xmlns:xsd="Company.Test3.Crm.Crm2Queue.Config.xsd"> 
    <organizations> 
     <organization name="Vanilla"> 
      <settings> 
       <ignoreemptyfields>true</ignoreemptyfields> 
       <throwerroronmissingconfiguration>true</throwerroronmissingconfiguration> 
      </settings> 
      <endpoints> 
       <endpoint> 
        <serviceUri>http://www.webservicex.net/usaddressverification.asmx</serviceUri> 
       </endpoint> 
      </endpoints> 
      <messages> 
       <message name="account"> 
        <field name="accountnumber" mappedname="State" /> 
        <field name="address1_county" mappedname="Zip" /> 
        <field name="address1_latitude" mappedname="City" /> 
       </message> 
      </messages> 
      <entities> 
       <entity name="account" messageschema="/XSD/.xsd" identifier="accountid"> 
        <events> 
         <event name="create" message="" /> 
         <event name="update" message="" /> 
         <event name="delete" message="" /> 
        </events> 
       </entity> 
      </entities> 
     </organization> 
    </organizations> 
</configuration> 

現在序列化類看起來如下:

[Serializable()] 
public class Organization 
{ 
    #region XmlIgnore 

    [XmlIgnore()] 
    public string Id { get; set; } 

    [XmlIgnore()] 
    public bool Checked { get; set; } 

    [XmlIgnore()] 
    public List<MappingMessage> mappingMessages { get; set; } 

    #endregion 

    #region Attributes 

    [XmlAttribute("name")] 
    public string Name { get; set; } 

    #endregion 

    #region Properties 

    [XmlElement("settings")] 
    public Settings Settings { get; set; } 
    public bool ShouldSerializeSettings() { return (Settings != null && (Settings.IgnoreEmptyFields.HasValue || Settings.ThrowErrorOnMissingConfiguration.HasValue)); } 

    [XmlArray("endpoints")] 
    [XmlArrayItem("endpoint")] 
    public List<Endpoint> Endpoints { get; set; } 
    public bool ShouldSerializeignoreEndpoints() { return (Endpoints.Count > 0); } 

    [XmlArray("messages")] 
    [XmlArrayItem("message")] 
    public List<MappingMessage> Messages 
    { 
     get { return mappingMessages.Where(mm => (mm.Fields.Where(fi => !string.IsNullOrEmpty(fi.MappedName)).ToList().Count > 0)).ToList(); } 
     set { mappingMessages = value; } 
    } 
    public bool ShouldSerializeFilledMappingMessages() { return (mappingMessages.Where(mm => (mm.Fields.Where(fi => !string.IsNullOrEmpty(fi.MappedName)).ToList().Count > 0)).ToList().Count > 0); } 
    //public bool ShouldSerializeMappingMessages() { return (MappingMessages.Where(mm=> (mm.Fields.Where(fi=> !string.IsNullOrEmpty(fi.MappedName)).ToList().Count > 0)).ToList().Count > 0); } 

    [XmlArray("entities")] 
    [XmlArrayItem("entity")] 
    public List<Entity> Entities { get; set; } 
    public bool ShouldSerializeEntities() { return (Entities.Count > 0); } 

    #endregion 

    #region Constructors 

    public Organization() 
    { 
     Settings = new Settings(); 
     Endpoints = new List<Endpoint>(); 
     mappingMessages = new List<MappingMessage>(); 
     Entities = new List<Entity>(); 
     Checked = false; 
    } 

    public Organization(string name) 
     : this() 
    { 
     Name = name; 
    } 

    public Organization(string id, string name) 
     : this(name) 
    { 
     Id = id; 
    } 

    #endregion 
} 


[Serializable()] 
public class MappingMessage 
{ 
    #region XmlIgnore 

    [XmlIgnore()] 
    public string EntityId { get; set; } 


    [XmlIgnore()] 
    public bool Checked { get; set; } 

    [XmlIgnore()] 
    public List<Field> Fields { get; set; } 

    #endregion 

    #region Attributes 

    [XmlAttribute("id")] 
    public string Id { get; set; } 

    [XmlAttribute("name")] 
    public string Name { get; set; } 

    #endregion 

    #region Properties 

    [XmlElement("field")] 
    public List<Field> SelectedFields 
    { 
     get 
     { 
      return Fields.Where(fi=> !string.IsNullOrEmpty(fi.MappedName)).ToList(); 
     } 
     set 
     { 
      Fields = value; 
     } 
    } 
    public bool ShouldSerializeSelectedFields() { return (SelectedFields.Count > 0); } 

    [XmlElement("subentity")] 
    public List<SubEntity> SubEntities { get; set; } 
    public bool ShouldSerializeSubEntities() { return (SubEntities.Count > 0); } 

    [XmlElement("parententity")] 
    public List<ParentEntity> ParentEntities { get; set; } 
    public bool ShouldSerializeParentEntities() { return (ParentEntities.Count > 0); } 

    #endregion 

    #region Constructors 

    public MappingMessage() 
    { 
     Checked = false; 
     Fields = new List<Field>(); 
     SubEntities = new List<SubEntity>(); 
     ParentEntities = new List<ParentEntity>(); 
    } 

    public MappingMessage(string entityId) 
     : this() 
    { 
     EntityId = entityId; 
    } 

    public MappingMessage(string entityId, string name) 
     : this(entityId) 
    { 
     Name = name; 
    } 

    #endregion 
} 

,而且我用反序列化,如下圖所示:

foreach (ZipEntry zipEntry in zipFile) 
       { 
        using (MemoryStream memoryStream = new MemoryStream()) 
        { 
         if (zipEntry.FileName.ToLower().EndsWith(".crm.crm2queue.config.xml")) 
         { 
          using (StreamReader streamReader = new StreamReader(memoryStream, Encoding.UTF8)) 
          { 
           zipEntry.Extract(memoryStream); 
           memoryStream.Position = 0; 
           XmlSerializer xmlSerializer = new XmlSerializer(typeof(Ciber.Crm.MappingCRMTo.Data.Configuration)); 
           configuration = (Configuration)xmlSerializer.Deserialize(streamReader); 
          } 
         } 
        } 
       } 

回答

1

解串器試圖填充列表returne public List<MappingMessage> Messages。爲了序列化器調用setter,必須將屬性類型更改爲不可變的集合類型,如MappingMessage[]

編輯:爲了看到這一點,您可以用帶有後臺字段的屬性替換實體自動屬性,並在getter和setter中設置斷點。你不應該在吸氣球員中掙扎,只能在吸氣球員身上。

+0

我確實已經確認之前,沒有調用setter,這對我現在是有意義的。它現在工作正常,謝謝你的幫助。 – 2013-02-20 15:27:08

相關問題