【问题标题】: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>

没有任何嵌套循环。

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

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

【问题讨论】:

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

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2014-08-18
    • 1970-01-01
    • 2021-10-12
    • 2021-01-25
    相关资源
    最近更新 更多