2014-09-02 57 views
0

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

示例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() 
+2

“C:\ COM \ DSS \ VXReports \ XSD文件\ EXAMPLE.CS” - “的.cs” 是一个不同寻常的扩展XML文件。 – 2014-09-03 00:14:17

+0

@AlexeiLevenkov,这是问题。我花了4个多小时试图弄清楚代码有什么问题,而且我甚至没有注意到文件名后面的.cs扩展名。谢谢。 – user3802347 2014-09-03 00:58:43

+0

诀窍是注意异常消息。这不是骗你。请记住,“一旦你消除了不可能的事物,无论如何,尽管不可能,是事实。因此,即使您的XML文档看起来很完美,您对XML序列化程序的输入也不是有效的XML文档。这意味着您完美的XML文档不是XML序列化程序的输入... – 2014-09-03 02:03:56

回答

0

我将文件扩展名从.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; } 
    } 
1

您的代码对我来说工作得很好。如果您在XML中具有该功能,请从顶部删除“示例XML文件”。如果你没有那个,你可能会遇到一些问题,如不可见的字符或者编码冲突。在SO上查看此链接,例如:Deserialization error in XML document(1,1)