简体   繁体   中英

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

I have an XML file that i am reading using XmlSerializer and StreamReader like this:

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

My class CarCollection is creating lists with the contents of the xml file.

Here are my 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;




    }

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

This is working just fine, and i'm getting the results i want, but now i need to put these 2 lists and the 3 fields in one dynamic list, how can i do it?

I'd like to know why you want to do what you describe.

You could add a property like this to get a list of everything:

        [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;
            }
        }

But there's no way to set it because how do you know which of the three string properties a string is. you could force it to put the three properties in the first three positions but thats just unpleasant code. You might think you could do this:

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

But you can't do that because the serializer has the same problem, its finds a string, how does it know which element or attribute to write in the xml?

One thing you could do is if Car and Address have the same base class you could do this:

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

Tell us more about what you are trying to achieve and maybe someone will help more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM