问题
I want to deserialize xml like this:
<Product>
<Classification>
<NewClassification>1</NewClassification>
<NewClassification>2</NewClassification>
<NewClassification>3</NewClassification>
<OldClassification>A</OldClassification>
<OldClassification>B</OldClassification>
</Classification>
</Product>
My classes are:
public class Product
{
public Classification Classification { get; set; }
}
public class Classification
{
[XmlArrayItem(typeof(int), ElementName = "NewClassification")]
public List<int> NewClassificationList { get; set; }
[XmlArrayItem(typeof(int), ElementName = "OldClassification")]
public List<int> OldClassificationList { get; set; }
}
Why this code is wrong?
回答1:
Apart from the "int literal" issue commented on by "Bears will eat you" above, you can't serialize your class in the way you want with what you've got (at least I can't think of a way to do it) without resorting to custom serialisation (e.g. implementing IXmlSerializable or some other method).
Your basic problem is that you have two seperate collections in Classification, which you want to appear as a single collection.
The IXmlSerialization route is pretty simple though. Just implement it as follows on your Classification class, and you'll get the desired result.
public class Classification : IXmlSerializable
{
public List<int> NewClassificationList { get; set; }
public List<string> OldClassificationList { get; set; }
public void WriteXml(System.Xml.XmlWriter writer)
{
foreach (var item in NewClassificationList)
{
writer.WriteElementString("NewClassification", item.ToString());
}
foreach (var item in OldClassificationList)
{
writer.WriteElementString("OldClassification", item.ToString());
}
}
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader reader)
{
throw new NotImplementedException();
}
}
Added ReadXml implementation
ReadXml can be implemented as follows:
public void ReadXml(System.Xml.XmlReader reader)
{
var product = new Product();
product.Classification = new Classification { NewClassificationList = new List<int>(), ldClassificationList = new List<string>()};
reader.ReadStartElement("Classification");
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
switch (reader.Name)
{
case "NewClassification":
product.Classification.NewClassificationList.Add(reader.ReadElementContentAsInt());
break;
case "OldClassification":
product.Classification.OldClassificationList.Add(reader.ReadElementContentAsString());
break;
default:
throw new NotSupportedException("Unsupported node: " + reader.Name);
}
}
reader.ReadEndElement();
}
来源:https://stackoverflow.com/questions/1874903/how-to-deserialize-xml-using-xmlarrayitem