代码之家  ›  专栏  ›  技术社区  ›  James

XmlSerializer-具有不同属性的同一元素

  •  0
  • James  · 技术社区  · 16 年前

    我的XML包含:

    <day p="d">
    <day p="n">
    

    为了使用XmlSerializer反序列化XML,需要向Day类添加哪些属性?

    2 回复  |  直到 6 年前
        1
  •  1
  •   erlando    16 年前

    以下装饰-

    [XmlType(TypeName="day")]
    public class Day
    {
        [XmlAttribute("p")]
        public string P { get; set; }
    }
    
    [XmlRoot("someObject")]
    public class SomeObject
    {
        [XmlArray("days")]
        public List<Day> Days { get; set; }
    }
    

    将序列化为:

    <someObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <days>
        <day p="n" />
        <day p="p" />
      </days>
    </someObject>
    

    希望你能有所收获。

    千电子伏

        2
  •  0
  •   leppie    16 年前
    [XmlElement("day")]
    public class Day
    {
      [XmlAttribute("p")]
      public string P {get;set;}
    }