我正在尝试序列化要传递给 Web 服务的对象并收到上述错误。我可以在调试时看到该值存在于对象本身中,但它似乎没有发现这一点:
string[] tradeAreas = new string[] {"Area1", "Area2", "Area3", "Area4"};
//RetrieveMarketResultsFor
ItemsChoiceType[] choices = new ItemsChoiceType[] { ItemsChoiceType.area };
MarketResultIdentifier mri = new MarketResultIdentifier
{
ItemsElementName = choices,
Items = tradeAreas,
ItemElementName = ItemChoiceType6.applyingDate,
Item = DateTime.Today.AddDays(-1)
};
var ser = new XmlSerializer(typeof(MarketResultIdentifier));
using (var stream = new FileStream("mri.xml", FileMode.Create))
ser.Serialize(stream, mri);
Web 服务生成的代码在上述代码中使用了以下类:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3130.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:openaccess")]
public partial class MarketResultIdentifier : OpenAccessAbstractObject {
private string[] itemsField;
private ItemsChoiceType[] itemsElementNameField;
private object itemField;
private ItemChoiceType6 itemElementNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("area", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("areaNames", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public string[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemsChoiceType[] ItemsElementName {
get {
return this.itemsElementNameField;
}
set {
this.itemsElementNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("applyingDate", typeof(System.DateTime), DataType="date")]
[System.Xml.Serialization.XmlElementAttribute("auctionIdentification", typeof(AuctionIdentification))]
[System.Xml.Serialization.XmlElementAttribute("deliveryDay", typeof(System.DateTime), DataType="date")]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType6 ItemElementName {
get {
return this.itemElementNameField;
}
set {
this.itemElementNameField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3130.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:openaccess", IncludeInSchema=false)]
public enum ItemsChoiceType {
/// <remarks/>
area,
/// <remarks/>
areaNames,
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3130.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:openaccess", IncludeInSchema=false)]
public enum ItemChoiceType6 {
/// <remarks/>
applyingDate,
/// <remarks/>
auctionIdentification,
/// <remarks/>
deliveryDay,
}
尝试 ser.serialize 时它失败了......
任何帮助表示赞赏:)
Solution原来这只是我愚蠢造成的!感谢@dbc 指出了这一点。我的 tradeAreas 数组有 4 个项目(我在这里使用了错误的数组),但它应该只有一个!