我为 XML 反序列化创建了以下类:
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class ProgrammingConfiguration
{
private uint versionField;
private byte[] eLFImageField;
private PreferenceType[] preferencesField;
private ProgrammingConfigurationProtectFlashAfterProgramming protectFlashAfterProgrammingField;
//XML version
public uint Version
{
get { return this.versionField; }
set { this.versionField = value; }
}
//ELF image
[System.Xml.Serialization.XmlElementAttribute(DataType = "base64Binary")]
public byte[] ELFImage
{
get { return this.eLFImageField; }
set { this.eLFImageField = value; }
}
//Preference
[System.Xml.Serialization.XmlArrayItemAttribute("Preference", IsNullable = false)]
public PreferenceType[] Preferences
{
get { return this.preferencesField; }
set { this.preferencesField = value; }
}
//Protect flash after programming
public ProgrammingConfigurationProtectFlashAfterProgramming ProtectFlashAfterProgramming
{
get { return this.protectFlashAfterProgrammingField; }
set { this.protectFlashAfterProgrammingField = value; }
}
}
//Preference Type
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class PreferenceType
{
private string tagField;
private PreferenceTypePrecedence precedenceField;
private ValueType valueField;
public string Tag
{
get { return this.tagField; }
set { this.tagField = value; }
}
public PreferenceTypePrecedence Precedence
{
get { return this.precedenceField; }
set { this.precedenceField = value; }
}
public ValueType Value
{
get { return this.valueField; }
set { this.valueField = value; }
}
}
//Precedence Type
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public enum PreferenceTypePrecedence
{
XML,
[System.Xml.Serialization.XmlEnumAttribute("Target,XML")]
TargetXML,
Target,
}
//Value Type
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ValueType
{
private string lengthField;
private object itemField;
private ItemChoiceType itemElementNameField;
[System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]
public string Length
{
get { return this.lengthField; }
set { this.lengthField = value; }
}
[System.Xml.Serialization.XmlElementAttribute("AutoIncrement", typeof(ValueTypeAutoIncrement))]
[System.Xml.Serialization.XmlElementAttribute("HexBinary", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Integer", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("String", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
public object Item
{
get { return this.itemField; }
set { this.itemField = value; }
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName
{
get { return this.itemElementNameField; }
set { this.itemElementNameField = value; }
}
}
//Autoincrement data
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ValueTypeAutoIncrement
{
private string minimumField;
private string maximumField;
private ValueTypeAutoIncrementOverflowBehaviour overflowBehaviourField;
public string Minimum
{
get { return this.minimumField; }
set { this.minimumField = value; }
}
public string Maximum
{
get { return this.maximumField; }
set { this.maximumField = value; }
}
public ValueTypeAutoIncrementOverflowBehaviour OverflowBehaviour
{
get { return this.overflowBehaviourField; }
set { this.overflowBehaviourField = value; }
}
}
//Overflow behaviour
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public enum ValueTypeAutoIncrementOverflowBehaviour
{
Wrap,
Exception,
}
//Data type
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema = false)]
public enum ItemChoiceType
{
AutoIncrement,
HexBinary,
Integer,
String,
}
//ProtectFlashAfterProgramming
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public enum ProgrammingConfigurationProtectFlashAfterProgramming
{
True,
False,
}
我想创建 2 个绑定源,一个用于 datagridview,一个用于文本框。我尝试了以下方法:
ProgrammingConfiguration pc = new ProgrammingConfiguration();
XmlSerializer serializer = new XmlSerializer(typeof(ProgrammingConfiguration));
//Reading the XML document requires a FileStream.
Stream reader = new FileStream(filename, FileMode.Open);
//Call the Deserialize method to restore the object's state.
pc = (ProgrammingConfiguration)serializer.Deserialize(reader);
reader.Close();
bindingsource.DataSource = pc;
bindingsource.DataMember = "Preferences";
bsV.DataSource = bindingsource;
bsV.DataMember = "ItemElementName";
该程序无法在 bsV.DataMember = "ItemElementName" 行运行。请有任何建议