C# .NET XML序列化错误:选择标识符的值无效或缺失
以下说明来自: 我尝试用XmlChoiceIdentifier属性序列化对象数组。但是很明显,序列化程序需要一个数组,而ObservableCollection将导致序列化程序抛出并出错(第一个链接)。我真的需要有一个ObservableCollection来保持UI的更新。因此,我试图让序列化引擎访问数组,而UI将以浪费资源的方式访问可观察的集合albiet 这是我的“解决方案” 使用此解决方案,我得到以下错误:C# .NET XML序列化错误:选择标识符的值无效或缺失,c#,.net,xml,wpf,serialization,C#,.net,Xml,Wpf,Serialization,以下说明来自: 我尝试用XmlChoiceIdentifier属性序列化对象数组。但是很明显,序列化程序需要一个数组,而ObservableCollection将导致序列化程序抛出并出错(第一个链接)。我真的需要有一个ObservableCollection来保持UI的更新。因此,我试图让序列化引擎访问数组,而UI将以浪费资源的方式访问可观察的集合albiet 这是我的“解决方案” 使用此解决方案,我得到以下错误: System.InvalidOperationException occu
System.InvalidOperationException occurred
HResult=-2146233079
Message=There was an error generating the XML document.
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
at Otometrics.ICSSuite.Reports.ViewModel.EditReportVM.<InitializeCommands>b__38(Object o) in
InnerException: System.InvalidOperationException
HResult=-2146233079
Message=Invalid or missing value of the choice identifier 'ItemTypeArray' of type 'ItemChoiceType[]'.
Source=Microsoft.GeneratedCode
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterCustomReportList.Write4_GroupAndItemsCollection(String n, String ns, GroupAndItemsCollection o, Boolean isNullable, Boolean needType)
尽管有一些关于此错误消息的正确答案。他们都没有工作。我希望我能获得荣誉,但唉,我真正聪明的团队领导发现了这一点。我的代码所发生的事情是,我们使用xsd2code为具有xmlChoiceIdentifier的模式生成vb。在我们的业务类中,当我们循环代码时,ItemChoiceType的对象在循环之外。因此,在验证xml时,第一个节点是好的,之后的每个节点都不好。虽然我有一个ItemsElementName,它有一个很好的值,但它只是一次迭代,其余的xml验证都会因为这个错误而失败 在创建xml和Viola时,我们在循环中移动了ItemsChoiceType!希望这有帮助。它可能与此堆栈溢出中的修复程序无关,但如果您的问题与我们的问题相同,它会给出相同的错误。您将看到这一页
快乐编码:)使用时的问题![XSD2代码与节点枚举有关。是的,它读取第一个节点,然后在其他节点上失败。对我来说,在我将GenerateOrderXmlAttributes和GenerateXMLAttributes更改为true并将CollectionObject类型更改为Array后,它就起作用了
using (StreamWriter writer = new StreamWriter(dlg.FileName))
{
ItemChoiceType[] ic = new ItemChoiceType[]
{
ItemChoiceType.Item,
ItemChoiceType.Macro
};
XmlSerializer xml = null;
group.ItemTypeArray = ic;
xml = new XmlSerializer(typeof(GroupAndItemsCollection));
xml.Serialize(writer, group);
writer.Close();
}
System.InvalidOperationException occurred
HResult=-2146233079
Message=There was an error generating the XML document.
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
at Otometrics.ICSSuite.Reports.ViewModel.EditReportVM.<InitializeCommands>b__38(Object o) in
InnerException: System.InvalidOperationException
HResult=-2146233079
Message=Invalid or missing value of the choice identifier 'ItemTypeArray' of type 'ItemChoiceType[]'.
Source=Microsoft.GeneratedCode
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterCustomReportList.Write4_GroupAndItemsCollection(String n, String ns, GroupAndItemsCollection o, Boolean isNullable, Boolean needType)
[XmlType(IncludeInSchema = false)]
public enum ItemChoiceType
{
[XmlEnumAttribute("Item")]
Item,
[XmlEnumAttribute("Macro")]
Macro
}
[Serializable()]
public class GroupAndItemsCollection
{
[XmlElementAttribute(IsNullable = false)]
[XmlIgnore]
public ItemChoiceType[] ItemTypeArray;
[XmlAttribute(AttributeName = "name")]
public string Group
{
get { return m_Group; }
set
{
if (m_Group == value)
return;
m_Group = value;
//OnPropertyChanged("Group");
}
}
private ListItemName[] m_itemsArray;
[XmlChoiceIdentifier("ItemTypeArray")]
[XmlElement(ElementName = "Item", Type = typeof(ObservableCollection<ListItemName>))]
[XmlElement(ElementName = "Macro", Type = typeof(ObservableCollection<ListItemName>))]
public ListItemName[] ItemsArray
{
get { return m_itemsArray; }
set { m_itemsArray = value; }
}
public GroupAndItemsCollection()
{
IsExpanded = true;
}
}
Message=Invalid or missing value of the choice identifier 'ItemTypeArray' of type 'ItemChoiceType[]'.