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 クラスには 1 つがあります (これは、XSD によって VS 2012 によって生成されたファイルです)。ファイル)。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にデータを入力するには、何を変更/修正する必要がありますか?