Home > Net >  want list Object [closed]
want list Object [closed]

Time:09-17

I want list object without any nested parameter;

controller ---

      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 File -

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

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

my output -

<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>

I want -

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

without any nested loop.

I am tried in different ways but I am not getting the correct way.

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

CodePudding user response:

Your class 'ZillowListingConnections' should be -

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

Sample Code

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();
}

Output

<?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>
  • Related