如何使用 C# 从其他列表创建动态列表

How to create a dynamic list from other lists with C#

我有一个 XML 文件,我正在使用 XmlSerializerStreamReader 阅读,如下所示:

CarCollection cars = null;
XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
StreamReader reader = new StreamReader(path)
cars = (CarCollection)serializer.Deserialize(reader);
reader.Close();

我的 class CarCollection 正在使用 xml 文件的内容创建列表。

这是我的 classes:

  [Serializable()]
    public class Car
    {
        [XmlElement("StockNumber")]
        public string StockNumber { get; set; }
        [XmlElement("Make")]
        public string Make { get; set; }
        [XmlElement("Model")]
        public string Model { get; set; }
    }

    [Serializable()]
    public class Address
    {
        [XmlAttribute("Type")]
        public string Type { get; set; }
        [XmlElement("Name")]
        public string Name { get; set; }
        [XmlElement("Street")]
        public string Street { get; set; }
        [XmlElement("City")]
        public string City { get; set; }
        [XmlElement("State")]
        public string State { get; set; }
        [XmlElement("Zip")]
        public string Zip { get; set; }
        [XmlElement("Country")]
        public string Country { get; set; }
    }

    [Serializable()]
    [XmlRoot("CarCollection")]
    public class CarCollection
    {
        [XmlAttribute("PurchaseOrderNumber")]
        public string PurchaseOrderNumber { get; set; }
        [XmlAttribute("OrderDate")]
        public string OrderDate { get; set; }

        [XmlElement("DeliveryNotes")]
        public string DeliveryNotes { get; set; }


        public List<Car> Cars;

        [XmlElement("Address")]
        public List<Address> Address;




    }

XML:

<?xml version="1.0" encoding="utf-8"?>
    <CarCollection PurchaseOrderNumber="99503" OrderDate="1999-10-20">
     <Address Type="Shipping">  
        <Name>Ellen Adams</Name>  
        <Street>123 Maple Street</Street>  
        <City>Mill Valley</City>  
        <State>CA</State>  
        <Zip>10999</Zip>  
        <Country>USA</Country>  
      </Address>  
     <Address Type="Billing">  
        <Name>Tai Yee</Name>  
        <Street>8 Oak Avenue</Street>  
        <City>Old Town</City>  
        <State>PA</State>  
        <Zip>95819</Zip>  
        <Country>USA</Country>  
      </Address>
     <DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes>  
     <Cars >
      <Car>
        <StockNumber>1020</StockNumber>
        <Make>Nissan</Make>
        <Model>Sentra</Model>
      </Car>
      <Car>
        <StockNumber>1010</StockNumber>
        <Make>Toyota</Make>
        <Model>Corolla</Model>
      </Car>
      <Car>
        <StockNumber>1111</StockNumber>
        <Make>Honda</Make>
        <Model>Accord</Model>
      </Car>
     </Cars>
    </CarCollection>

这工作得很好,我得到了我想要的结果,但现在我需要将这 2 个列表和 3 个字段放在一个动态列表中,我该怎么做?

我想知道你为什么想做你描述的事情。

您可以像这样添加 属性 以获得所有内容的列表:

        [XmlIgnore]
        public List<object> Things
        {
            get
            {
                var ret = new List<object>();
                if (PurchaseOrderNumber != null)
                    ret.Add(PurchaseOrderNumber);
                if (OrderDate != null)
                    ret.Add(OrderDate);
                if (DeliveryNotes != null)
                    ret.Add(DeliveryNotes);
                if(Cars != null)
                    ret.AddRange(Cars);
                if(Address != null)
                    ret.Add(Address);
                return ret;
            }
        }

但是没办法设置,因为你怎么知道一个字符串是三个字符串属性中的哪一个。您可以强制它将三个属性放在前三个位置,但那只是令人不快的代码。你可能认为你可以这样做:

[XmlAttribute("PurchaseOrderNumber", typeof(string))]
[XmlAttribute("OrderDate", typeof(string))]
[XmlElement("DeliveryNotes", typeof(string))]
List<object> Things;

但是你不能那样做,因为序列化器有同样的问题,它找到一个字符串,它怎么知道要在 xml 中写入哪个元素或属性?

您可以做的一件事是,如果 Car 和 Address 具有相同的基数 class,您可以这样做:

[XmlElement("Car", typeof(Car))]
[XmlElement("Address", typeof(Address))]
public List<BaseClass> Things {get;set;}

告诉我们更多关于您想要实现的目标,也许有人会提供更多帮助。