1

I am new for XML, basically I have class like this:

public class Items:List<string>
{
    private string _name;
    public string Product
    {
    get {return _name;}
    set {_name=value;}
    }
}

I want to serialize object base on this class like this:

  <Items>
      <Product>product name</Product>
      <Item> A1 </Item>
      <Item> A2 </Item>
      <Item> A3 </Item>
      <Item> A4 </Item>
      <Item> A5 </Item>
   </Items>

My question is when I try to serialize this object,XML serialize program ignore Product element, only got this XML data:

<Items>
   <Item> A1 </Item>
   <Item> A2 </Item>
   <Item> A3 </Item>
   <Item> A4 </Item>
   <Item> A5 </Item>
</Items>

Anyone can help me to get right format XML doc.

4

1 回答 1

1

XmlSerializer (and a lot of other code) treats things as either an item or (exclusive) a list. You should not subclass a list and properties: you should have a type that has a list and has a property.

[XmlRoot("Items")]
public class Foo {
    public string Product {get;set;}

    private readonly List<string> items = new List<string>();
    [XmlElement("Item")]
    public List<string> Items {get{return items;}}
}
于 2013-09-18T21:22:16.240 回答