使用 VS 2012 和 .NET 4.0,我有以下代码可以正确反序列化 XML 文件:
XML 文件:
<?xml version="1.0"?>
<Codes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<Code>
<AccpacCode>CA9998</AccpacCode>
<LAC>90699999999999999</LAC>
<SCSCode>SDI</SCSCode>
</Code>
<Code>
<AccpacCode>ORWC</AccpacCode>
<LAC>94199999999999999</LAC>
<SCSCode>WC</SCSCode>
</Code>
<Code>
<AccpacCode>AK9999</AccpacCode>
<LAC>90299999999999999</LAC>
<SCSCode>UI</SCSCode>
<ParentEmployerAccpacCode>AKSUTA</ParentEmployerAccpacCode>
</Code>
<Codes>
XSD 文件:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Codes" xmlns="SerializeObservableCollection" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Codes" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Code">
<xs:complexType>
<xs:sequence>
<xs:element name="AccpacCode" type="xs:string" minOccurs="0" />
<xs:element name="LAC" type="xs:string" minOccurs="0" />
<xs:element name="SCSCode" type="xs:string" minOccurs="0" />
<xs:element name="ParentEmployerAccpacCode" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
C#代码:
try
{
XmlSerializer _serializer = new XmlSerializer(typeof(Codes));
// A file stream is used to read the XML file into the ObservableCollection
using (StreamReader _reader = new StreamReader(@"LocalCodes.xml"))
{
var codes = _serializer.Deserialize(_reader) as Codes;
}
}
catch (Exception ex)
{
// Catch exceptions here
}
我想将反序列化的结果放入 ObservableCollection 并找到说明以下内容应该有效的示例:
ObservableCollection<Codes> CodeCollection;
...
try
{
XmlSerializer _serializer = new XmlSerializer(typeof(ObservableCollection<Codes>));
// A file stream is used to read the XML file into the ObservableCollection
using (StreamReader _reader = new StreamReader(@"LocalCodes.xml"))
{
CodeCollection = _serializer.Deserialize(_reader) as ObservableCollection<Codes>;
}
}
当我运行此代码时,我收到一条错误消息"There is an error in XML document (2, 2)."
和一个内部异常,"<Codes xmlns=''> was not expected."
我看到提到需要一个默认构造函数来完成这项工作,并且 Codes.cs 类确实有一个(它是由 VS 2012 由 XSD 生成的文件文件)。这是 Codes.cs 文件的片段:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18051
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#pragma warning disable 1591
namespace SerializeObservableCollection {
/// <summary>
///Represents a strongly typed in-memory cache of data.
///</summary>
[global::System.Serializable()]
[global::System.ComponentModel.DesignerCategoryAttribute("code")]
[global::System.ComponentModel.ToolboxItem(true)]
[global::System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedDataSetSchema")]
[global::System.Xml.Serialization.XmlRootAttribute("Codes")]
[global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.DataSet")]
public partial class Codes : global::System.Data.DataSet {
private CodeDataTable tableCode;
private global::System.Data.SchemaSerializationMode _schemaSerializationMode = global::System.Data.SchemaSerializationMode.IncludeSchema;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
public Codes() {
this.BeginInit();
this.InitClass();
global::System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new global::System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
base.Tables.CollectionChanged += schemaChangedHandler;
base.Relations.CollectionChanged += schemaChangedHandler;
this.EndInit();
}
我需要更改/修复什么才能使其正常工作并填充 ObservableCollection?