0

使用 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?

4

2 回答 2

2

@BrianKE我非常怀疑这仍然对您有用,但也许对其他人有用。我今天遇到了和你一样的问题。花了我半天时间才弄明白。

首先,您看到的消息是因为您指定的类型......

XmlSerializer _serializer = new XmlSerializer(typeof(ObservableCollection<Codes>));

..不是 ObservableCollection。如果类型错误,您会收到此错误。代码必须是可观察的集合,您的语句才能正常工作。

您需要的是根元素,目前 Codes 是您的根元素,因此您需要一个仅作为根的 Class,然后在此之下有许多 Code 元素。例如

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class MIGRATION : object, INotifyPropertyChanged, INotifyPropertyChanging
{

    private string projectNameField;

    private ObservableCollection<FileDescriptions> fileDescriptionsField;

    //private FileDescriptions[] fileDescriptionsField;

    /// <remarks/>
    //[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [XmlElement("ProjectName")]
    public string ProjectName
    {
        get
        {
            return this.projectNameField;
        }
        set
        {
            this.RaisePropertyChanging("ProjectName");
            this.projectNameField = value;
            this.RaisePropertyChanged("ProjectName");
        }
    }

    /// <remarks/>
    [XmlElement("FileDescriptions")]
    public ObservableCollection<FileDescriptions> FileDescriptions 
    { 
        get
        {
            return this.fileDescriptionsField;
        }
        set
        {
            this.fileDescriptionsField = value;
            this.RaisePropertyChanged("FileDescriptions");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null))
        {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }

    public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;

    protected void RaisePropertyChanging(string propertyName)
    {
        System.ComponentModel.PropertyChangingEventHandler propertyChanging = this.PropertyChanging;
        if ((propertyChanging != null))
        {
            propertyChanging(this, new System.ComponentModel.PropertyChangingEventArgs(propertyName));
        }
    }
}

所以 Observable 集合位于反序列化类中的元素上。我知道你在说什么,我见过你描述的例子,但我不知道那是什么。据我所知,反序列化与可观察的集合无关。除非他们有不止一个根元素?或者,当您不反序列化整个 XML,而只是反序列化其中的一部分时,也许是有意义的。

于 2017-07-10T14:51:14.647 回答
-1

反序列化只能用于之前使用序列化序列化的内容。否则,你只是在玩火。

您可以使用 XDocument 轻松打开和遍历 XML 文档。

XDocument xml = XDocument.Load(filename);
var collection = new ObservableCollection<Code>();
foreach (XElement e in xml.Elements("code"))
{
   collection.Add(new Code() { AccpacCode = e.Element("AccpacCode").Value,
                               LAC = e.Element("LAC").Value });
}

您可以轻松地迭代所有 xml.Elements 并启动您添加到 ObservableCollection 的代码对象。虽然在加载过程中界面可能会闪烁一点。

于 2013-10-17T17:26:12.617 回答