代码之家  ›  专栏  ›  技术社区  ›  Pablo Retyk

当对象将序列化时,如何指定元素的名称

  •  1
  • Pablo Retyk  · 技术社区  · 17 年前

    我有以下课程

        [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 (无资本) 我知道我只能为根用户指定,但如何为内部成员指定?

    1 回复  |  直到 17 年前
        1
  •  3
  •   Marc Gravell    17 年前
    [XmlType("webSite")]
    public class WebSite {...}
    

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

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