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

时间:2022-06-01 16:39:07

I have the following class

我有以下课程

    [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; }
}

this is serialized to

这是序列化的

 <?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" />

this is almost ok but I want that WebSite(With a capital) will be webSite (no capital) I know I can specify this only for the root, but how can I for an internal member?

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

1 个解决方案

#1


3  

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

or to control a collection property on a wrapper class:

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

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

#1


3  

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

or to control a collection property on a wrapper class:

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

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