如何在对象序列化时指定元素的名称

时间:2008-11-17 14:01:07

标签: c# .net serialization xml-serialization

我有以下课程

    [XmlRoot(ElementName= "webSites")] //No capital w at the beginning
public class WebSites : List<WebSite>
{

}

public class WebSite
{
    [XmlAttribute("name")]
    public string Name { set; get; }
    [XmlAttribute("url")]
    public String Url { set; get; }
}

这被序列化为

 <?xml version="1.0" encoding="DOS-862"?>
<webSites xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http:
//www.w3.org/2001/XMLSchema">
  <WebSite name="nice website" url="mydomain.com" />

这几乎没问题,但我希望WebSite(有资本)将是webSite(没有资本) 我知道我只能为root指定这个,但我怎么能为内部成员?

1 个答案:

答案 0 :(得分:3)

[XmlType("webSite")]
public class WebSite {...}

或控制包装类的集合属性:

[XmlArrayItem("webSite")]
[XmlArray("sites")]
public WebSites Sites { get; set; }