繁体   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