我想反序列化和使用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()
“C:\ COM \ DSS \ VXReports \ XSD文件\ EXAMPLE.CS” - “的.cs” 是一个不同寻常的扩展XML文件。 – 2014-09-03 00:14:17
@AlexeiLevenkov,这是问题。我花了4个多小时试图弄清楚代码有什么问题,而且我甚至没有注意到文件名后面的.cs扩展名。谢谢。 – user3802347 2014-09-03 00:58:43
诀窍是注意异常消息。这不是骗你。请记住,“一旦你消除了不可能的事物,无论如何,尽管不可能,是事实。因此,即使您的XML文档看起来很完美,您对XML序列化程序的输入也不是有效的XML文档。这意味着您完美的XML文档不是XML序列化程序的输入... – 2014-09-03 02:03:56