簡體   English   中英

沒有頂級arrayElement的Xml數組反序列化為C#對象

[英]Xml Array Deserialization to C# object without top level arrayElement

我有以下Xml:

<Head>
<Name>someName</Name>
<Config>
  <Section name='One'/>  
  <Section name='Two'/>
</Config>
</Head>

我希望它映射以下對象結構:

public class Head{
   public String Name {get;set;}

   public Config Config {get;set;}       

}

public class Config
{       
    public Section[] Sections { get; set; }
}

public class Section
{
    [XmlAttribute(AttributeName="name")]
    public String Name { get; set; }

}

我如何只使用屬性(如果可能的話)來執行此操作,而又不添加頂級<Sections>到<Section>元素上方, 又不保留類結構。 我已經嘗試過XmlArrayItem,但是我無法獲得section元素。

在提出問題之前,我已經嘗試了XmlArrayAttributeXmlArraItemAttribute及其所有可能的namedProperty組合,並且要使用的屬性只是XmlElement 所以我這樣管理:

剛剛在Config類的Sections屬性頂部添加了[XmlElement(ElementName =“ Section”)] 在下面更新:

  public class Head{
     public String Name {get;set;}

     public Config Config {get;set;}       

  }

  public class Config
  {       
      [XmlElement(ElementName="Section")]
      public Section[] Sections { get; set; }
  }

  public class Section
  {
      [XmlAttribute(AttributeName="name")]
      public String Name { get; set; }

  }

暫無
暫無

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

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