[英]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: 我有一个使用
XmlSerializer
和StreamReader
读取的XML文件,如下所示:
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. 我的班级
CarCollection
正在创建带有xml文件内容的列表。
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:
<?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? 这工作得很好,我正在获得想要的结果,但是现在我需要将这2个列表和3个字段放在一个动态列表中,我该怎么做?
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? 但是您不能这样做,因为序列化器有同样的问题,它找到一个字符串,它如何知道要在xml中写入哪个元素或属性?
One thing you could do is if Car and Address have the same base class you could do this: 您可以做的一件事是,如果Car和Address具有相同的基类,则可以这样做:
[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. 告诉我们更多有关您要实现的目标的信息,也许有人会提供更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.