反序列化XML无效操作异常

本文关键字:异常 操作 无效 XML 反序列化 | 更新日期: 2023-09-27 18:20:21

我正在尝试使用使用xsd.exe工具创建的对象来反序列化和xml文件。当我尝试运行示例应用程序时,我得到了以下异常,InvalidOperationException-XML文档(1,1)中存在错误。我在这里做错了什么?这是我的代码:

示例XML文件

<?xml version="1.0" encoding="utf-8"?>
<patient>
 <pr>
<fname>Cindy </fname>
<lname>Knapp </lname>
<id>1 </id>
 </pr>
 <pr>
<fname>Regie </fname>
<lname>Miller </lname>
<id>2 </id>
 </pr>
<ad>
  <id>1</id>
  <streetnumber>1200 NW</streetnumber>
  <city>Miami </city>
  <state>FL</state>
  <zip>34488</zip>
</ad>
<ad>
  <id>2</id>
  <streetnumber>1299 NE</streetnumber>
  <city>NY </city>
  <state>NY</state>
  <zip>9032</zip>
</ad>
</patient>

这是生成的类

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18444
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
// 
// This source code was auto-generated by xsd, Version=4.0.30319.17929.
// 

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[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 patient {
    private patientPR[] prField;
    private patientAD[] adField;
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("pr")]
    public patientPR[] pr {
        get {
            return this.prField;
        }
        set {
            this.prField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ad")]
    public patientAD[] ad {
        get {
            return this.adField;
        }
        set {
            this.adField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class patientPR {
    private string fnameField;
    private string lnameField;
    private int idField;
    /// <remarks/>
    public string fname {
        get {
            return this.fnameField;
        }
        set {
            this.fnameField = value;
        }
    }
    /// <remarks/>
    public string lname {
        get {
            return this.lnameField;
        }
        set {
            this.lnameField = value;
        }
    }
    /// <remarks/>
    public int id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class patientAD {
    private int idField;
    private string streetnumberField;
    private string cityField;
    private string stateField;
    private ushort zipField;
    /// <remarks/>
    public int id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
    /// <remarks/>
    public string streetnumber {
        get {
            return this.streetnumberField;
        }
        set {
            this.streetnumberField = value;
        }
    }
    /// <remarks/>
    public string city {
        get {
            return this.cityField;
        }
        set {
            this.cityField = value;
        }
    }
    /// <remarks/>
    public string state {
        get {
            return this.stateField;
        }
        set {
            this.stateField = value;
        }
    }
    /// <remarks/>
    public ushort zip {
        get {
            return this.zipField;
        }
        set {
            this.zipField = value;
        }
    }
}

在主函数中,我尝试反序列化xml文件。

 class Program
    {
        static void Main(string[] args)
        {
            patient pr = new patient();
            XmlSerializer serializer = new XmlSerializer(typeof(patient));
            FileStream myStream = new FileStream(@"C:'com'dss'VXReports'XSD Document'EXAMPLE.CS", FileMode.Open);
            pr = (patient)serializer.Deserialize(myStream);
            string kr = string.Empty;
        }
    }
Stack Trace:
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
   at TestXMLFile.Program.Main(String[] args) in c:'com'dss'VXReports'Clinical Quality Measure'TestXMLFile'TestXMLFile'Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

反序列化XML无效操作异常

您的代码对我来说非常好。如果您的XML中有"示例XML文件",请从顶部删除。如果你没有,你可能会遇到一些问题,比如看不见的字符,或者可能是编码冲突。请参阅SO上的此链接,例如:XML文档(1,1)中的反序列化错误

我将文件扩展名从in从.cs更改为.xml。我还在项目中创建了一些分部类,并为这些类添加了一些xml装饰。使用xsd.exe自动生成的类中没有任何更改。我的主类

 static void Main(string[] args)
        {
            patient pr = new patient();
            XmlSerializer serializer = new XmlSerializer(typeof(patient));
            FileStream myStream = new FileStream(@"C:'com'dss'VXReports'XSD Document'EXAMPLE.XML", FileMode.Open);
            pr = (patient)serializer.Deserialize(myStream);
            string kr = string.Empty;
        }

新的分部类:

 [Serializable, XmlRoot("patient")]
    public partial class patient
    {
        [XmlElement("pr")]
        public patientPR[] pr { get; set; }
        [XmlElement("ad")]
        public patientAD[] ad { get; set; }
    }
    [Serializable, XmlType("pr")]
    public partial class patientPR
    {
        [XmlElement("fname")]
        public string fname { get; set; }
        [XmlElement("lname")]
        public string lname { get; set; }
        [XmlElement("id")]
        public int id { get; set; }
    }
    [Serializable, XmlType("ad")]
    public partial class patientAD
    {
        [XmlElement("id")]
        public int id { get; set; }
        [XmlElement("streetnumber")]
        public string streetnumber { get; set; }
        [XmlElement("city")]
        public string city { get; set; }
        [XmlElement("state")]
        public string state { get; set; }
        [XmlElement("zip")]
        public ushort zip { get; set; }
    }