想要清单对象

want list Object

我想要没有任何嵌套参数的列表对象;

控制器---

      var ListingIdList = new List<ListingId>();
     foreach (var itemCommunityPlan in community.Plan)
     {
         foreach (var itemSpec in itemCommunityPlan.Spec)
         {
             if (!string.IsNullOrEmpty(itemSpec.SpecMLSNumber))
             {
                 ListingIdList.Add(new ListingId { ListingIds = itemSpec.SpecMLSNumber });
             }
         }
     }
     if (ListingIdList.Any())
     {
         community.ListingConnections = new ZillowListingConnections { MlsIdentifier = community.MLSName, ListingId = ListingIdList.ToList() };
     }
     else
         community.ListingConnections = null;

class 文件 -

 public class ZillowListingConnections
    {
        public string MlsIdentifier { get; set; }
        public List<ListingId> ListingId { get; set; }
    }

    public class ListingId 
    {
        public string ListingIds { get; set; }
    }

我的输出-

<ListingConnections>
    <MlsIdentifr>Test SS</MlsIdentifier>
    <ListingId>
        <ListingId>
            <ListingIds>AAA</ListingIds>
        </ListingId>
        <ListingId>
            <ListingIds>BB</ListingIds>
        </ListingId>
        <ListingId>
            <ListingIds>CC</ListingIds>
        </ListingId>
        <ListingId>
            <ListingIds>DD</ListingIds>
        </ListingId>
    </ListingId>
</ListingConnections>

我要-

<ListingConnections>
   <MlsIdentifier>Test ss</MlsIdentifier>
   <ListingId>AA</ListingId>
   <ListingId>BB</ListingId>
</ListingConnections>

没有任何嵌套循环。

我尝试过不同的方法,但我没有找到正确的方法。

................................................ .....................

你的class'ZillowListingConnections'应该是-

[XmlRoot(ElementName = "ListingConnections")]
public class ListingConnections
{
    [XmlElement(ElementName = "MlsIdentifier")]
    public string MlsIdentifier { get; set; }
    [XmlElement(ElementName = "ListingId")]
    public List<string> ListingId { get; set; }
}

示例代码

public static void Main(string[] args)
{
    var listingConnections = new ListingConnections();
    listingConnections.MlsIdentifier = "test";

    var ListingIds = new List<string>();
    ListingIds.Add("1");
    ListingIds.Add("2");
    ListingIds.Add("3");

    listingConnections.ListingId = ListingIds;

    var xmlSerializer = new XmlSerializer(typeof(ListingConnections));
    xmlSerializer.Serialize(Console.Out, listingConnections);

    Console.WriteLine();
}

输出

<?xml version="1.0" encoding="utf-16"?>
<ListingConnections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MlsIdentifier>test</MlsIdentifier>
  <ListingId>1</ListingId>
  <ListingId>2</ListingId>
  <ListingId>3</ListingId>
</ListingConnections>