想要列表对象[关闭]

Posted

技术标签:

【中文标题】想要列表对象[关闭]【英文标题】:want list Object [closed] 【发布时间】:2021-09-16 07:17:03 【问题描述】:

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

控制器---

      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;

类文件-

 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>

没有任何嵌套循环。

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

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

【问题讨论】:

请包括代码、错误消息和其他基于文本的信息作为文本,而不是屏幕截图。 我们需要将代码视为文本而不是屏幕截图,如果您不想显示某些内容而不是尝试隐藏它,我们也会建议您更改值。 请查看 【参考方案1】:

您的课程“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>

【讨论】:

以上是关于想要列表对象[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 master 上构建所有 git 提交的列表? [关闭]

AutoMapper - 整数列表到对象列表[关闭]

如何遍历对象列表并将特定值合并到一个大列表[关闭]

Java流从列表中创建一个对象[关闭]

TypeScript:标头/值列表->列出ob对象[关闭]

TypeError:'int'对象不支持嵌套python列表中的项目分配[关闭]