我正在尝试使用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;
}
}
}
在main函数中,我尝试反序列化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()
答案 0 :(得分:1)
您的代码对我来说非常好。如果您在XML中有这个,请从顶部删除“示例XML文件”。如果你没有那个,你可能会遇到一些问题,如不可见的字符或可能的编码冲突。在SO上查看此链接,例如:Deserialization error in XML document(1,1)
答案 1 :(得分: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; }
}