deserialize streamReader as Observablecollection
-
Monday, January 07, 2013 8:49 PM
Hope someone can help me resolve below error.
<The thread '<No Name>' (0x1d2c) has exited with code 0 (0x0).
A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen>
This error occurs when I try to deserialize streamReader as Observablecollection. See code below:
class InstrumentListViewModel { private ObservableCollection<Instrument> _InstrumentList = new ObservableCollection<Instrument>(); public ObservableCollection<Instrument> InstrumentList { get { return _InstrumentList; } set {_InstrumentList = value;} } public InstrumentListViewModel() { XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<Instrument>)); using (StreamReader sr = new StreamReader(@"C:\Instruments.xml")) { InstrumentList = xs.Deserialize(sr) as ObservableCollection<Instrument>; } }
}
<?xml version="1.0" encoding="utf-8"?> <Instruments> <Instrument> <Alias>Source1"</Alias> <Type>Source</Type> <InterfaceID>0</InterfaceID> <IterfaceType>GPIB</IterfaceType> <Address>9</Address> <Slot></Slot> <SerialNumber></SerialNumber> <CalibrationDate></CalibrationDate> </Instrument> <Instrument> <Alias>Load1"</Alias> <Type>Load</Type> <InterfaceID>0</InterfaceID> <IterfaceType>GPIB</IterfaceType> <Address>10</Address> <Slot></Slot> <SerialNumber>1234</SerialNumber> <CalibrationDate></CalibrationDate> </Instrument> </Instruments>
public class Instrument //: ObservableObject, IComparable<Instrument> { public enum InstrumentType { Source, Load, Oscilloscope, TemperatureChamber, PowerMeter, DVM,DataAcqSwitchUnit, DACCard, Thermometer,FunctionGenerator, LCRMeter} public enum InstrumentInterface { GPIB,USB,ASRL} // The following properties are wrapping an array of strings [Category("Common")] [DisplayName("Type")] public InstrumentType Type {get; set;} [Category("Common")] [DisplayName("Alias")] public string Alias { get; set; } [Category("Interface")] [DisplayName("Interface Type")] public InstrumentInterface IterfaceType { get; set; } [Category("Interface")] [DisplayName("Interface ID")] [Description("Numerical value of 0 through 30.")] public int InterfaceID { get; set; } [Category("Interface")] [DisplayName("Address")] [Description("Enter Instrument Address")] public string Address { get; set; } [Category("Interface")] [DisplayName("Slot")] [Description("Enter appropriate Channel, Bay, or slot position.")] public int Slot { get; set; } [Category("Other")] [DisplayName("Serial Number")] public string SerialNumber { get; set; } [Category("Other")] [DisplayName("Calibration Date")] public string CalibrationDate { get; set; } }Thanks,
Wes
wesley
All Replies
-
Tuesday, January 08, 2013 4:58 PM
Figured it out. Made changes to xml file.
In root node, changed <Instruments> to <ArrayOfInstrument> and all child nodes must contain a value.
<?xml version="1.0" encoding="utf-8"?> <ArrayOfInstrument> <Instrument> <Alias>Source1"</Alias> <Type>Source</Type> <InterfaceID>0</InterfaceID> <IterfaceType>GPIB</IterfaceType> <Address>9</Address> <Slot>0</Slot> <SerialNumber>0</SerialNumber> <CalibrationDate>0</CalibrationDate> </Instrument> <Instrument> <Alias>Load1"</Alias> <Type>Load</Type> <InterfaceID>0</InterfaceID> <IterfaceType>GPIB</IterfaceType> <Address>10</Address> <Slot>0</Slot> <SerialNumber>1234</SerialNumber> <CalibrationDate>0</CalibrationDate> </Instrument> </ArrayOfInstrument>
-
Wednesday, January 09, 2013 5:35 AMGood
NEU_ShieldEdge
-
Wednesday, January 09, 2013 6:57 PMIs it possible not to use ArrayOf?
wesley

