Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义序列化嵌套集合对象_C#_Restsharp_Xmlserializer - Fatal编程技术网

C# 自定义序列化嵌套集合对象

C# 自定义序列化嵌套集合对象,c#,restsharp,xmlserializer,C#,Restsharp,Xmlserializer,我正在尝试用RestSharp的XmlSerializer序列化C#对象 我的模型是: public class Person { public string Name { get; set; } public List<Address> Addresses { get; set; } } public class Address { public string City { get; set; } } 公共类人物 { 公共字符串名称{get;set;}

我正在尝试用RestSharp的XmlSerializer序列化C#对象

我的模型是:

public class Person
{
    public string Name { get; set; }
    public List<Address> Addresses { get; set; }
}

public class Address
{
    public string City { get; set; }
}
公共类人物
{
公共字符串名称{get;set;}
公共列表地址{get;set;}
}
公共课堂演讲
{
公共字符串City{get;set;}
}
客户端代码是这样的,这里没有什么特别之处

        List<Address> addresses = new List<Address>();
        addresses.Add(new Address { City = "NY" });
        addresses.Add(new Address { City = "Istanbul" });

        Person person = new Person { Name = "Mike", Addresses = addresses };

        Console.WriteLine(new RestSharp.Serializers.XmlSerializer().Serialize(person));
列表地址=新列表();
地址。添加(新地址{City=“NY”});
地址.添加(新地址{City=“伊斯坦布尔”});
人员人员=新人员{Name=“Mike”,地址=地址};
WriteLine(新的RestSharp.Serializers.XmlSerializer().Serialize(person));
正如预期的那样,输出如下:

<Person>
  <Name>Mike</Name>
  <Addresses>
    <Address>
      <City>NY</City>
    </Address>
    <Address>
      <City>Istanbul</City>
    </Address>
  </Addresses>
</Person>

迈克
纽约
伊斯坦布尔
有没有办法得到以下结果?(“地址”元素名称已删除)


迈克
纽约
伊斯坦布尔

addxmlement:[xmlement(“Address”)]公共列表地址{get;set;}我认为这适用于System.Xml.Serialization命名空间。RestSharp忽略该属性。尝试将[Serializable]属性添加到类中。使用内置属性,然后应用
[XmlElement(“Address”)]
,如图所示。事实上,这是重复的吗?我想不出使用DotNetXmlSerializer的主意。谢谢你提到那件事。我认为RestSharp的原生xml序列化程序不支持某些属性。
   <Person>
      <Name>Mike</Name>
        <Address>
          <City>NY</City>
        </Address>
        <Address>
          <City>Istanbul</City>
        </Address>
    </Person>