[英]Deserialize Xml Using XmlReader and Class from xsd.exe
好的,我在嘗試學習XmlSerializer的過程中遇到了一些障礙。 我已經遵循了所有建議的步驟,但我的程序沒有返回任何內容,或者返回null。 我創建了一個XML文件,如下所示:
<?xml version='1.0' encoding='UTF-8' ?>
<WeeklyJobs>
<DailyJobs Date = "02/03/2012"/>
<DailyJobs Date = "02/04/2012" TotalJobs = "2">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
<Job JobName = "Job Name" Description = "Description"/>
</Jobs>
</DailyJobs>
<DailyJobs Date = "02/05/2012" TotalJobs = "1">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
</Jobs>
</DailyJobs>
<DailyJobs Date = "02/06/2012" TotalJobs = "2">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
<Job JobName = "Job Name" Description = "Description"/>
</Jobs>
</DailyJobs>
<DailyJobs Date = "02/07/2012"/>
<DailyJobs Date = "02/08/2012" TotalJobs = "2">
<Jobs>
<Job JobName = "Job Name" Description = "Description"/>
<Job JobName = "Job Name" Description = "Description"/>
</Jobs>
</DailyJobs>
</WeeklyJobs>
然后我使用xsd.exe生成.xsd文件,它是:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="WeeklyJobs" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="WeeklyJobs" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="DailyJobs">
<xs:complexType>
<xs:sequence>
<xs:element name="Jobs" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Job" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="JobName" type="xs:string" />
<xs:attribute name="Description" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Date" type="xs:string" />
<xs:attribute name="TotalJobs" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
創建架構后,我再次使用xsd.exe為我自動生成類,即:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[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 WeeklyJobs
{
private WeeklyJobsDailyJobs[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DailyJobs", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public WeeklyJobsDailyJobs[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class WeeklyJobsDailyJobs
{
private WeeklyJobsDailyJobsJobsJob[][] jobsField;
private string dateField;
private string totalJobsField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("Job", typeof(WeeklyJobsDailyJobsJobsJob), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public WeeklyJobsDailyJobsJobsJob[][] Jobs
{
get
{
return this.jobsField;
}
set
{
this.jobsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Date
{
get
{
return this.dateField;
}
set
{
this.dateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string TotalJobs
{
get
{
return this.totalJobsField;
}
set
{
this.totalJobsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class WeeklyJobsDailyJobsJobsJob
{
private string jobNameField;
private string descriptionField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string JobName
{
get
{
return this.jobNameField;
}
set
{
this.jobNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Description
{
get
{
return this.descriptionField;
}
set
{
this.descriptionField = value;
}
}
}
在此之后,我將.CS文件添加到我的項目中,並創建了一個帶有TextBox的簡單winform,以便在我對一些xml進行反序列化后顯示一些數據。 就像我說程序啟動一樣,TextBox中沒有顯示任何內容,也沒有拋出異常。 這是我的winform:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string getExample()
{
XmlSerializer serializer = new XmlSerializer(typeof(WeeklyJobs));
WeeklyJobs jobs;
string xml = @"<?xml version = ""1.0""?>"
+ @"<WeeklyJobs>"
+ @"<DailyJobs Date = ""02/03/2012""/>"
+ @"<DailyJobs Date = ""02/04/2012"" TotalJobs = ""2"">"
+ @"<Jobs>"
+ @"<Job JobName = ""Job Name"" Description = ""Description""/>"
+ @"<Job JobName = ""Job Name"" Description = ""Description""/>"
+ @"</Jobs>"
+ @"</DailyJobs>"
+ @"</WeeklyJobs>";
// Create an XmlTextReader
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
jobs = (WeeklyJobs)serializer.Deserialize(reader);
}
return jobs.Items[0].Date;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = getExample();
}
}
我有更簡單的xml示例可以工作,但我第二次嘗試為我的xml添加一些復雜性我失敗了。 我必須弄清楚如何使用我在這里的xml。 我很感激幫助! 感謝大家!
好吧,第一個問題是XmlSerializer構造函數正在引發一個未處理的異常,但這是以某種方式被吞噬。 這解釋了文本框為空的原因。 我將調試器設置為中斷所有CLR異常,並發現構造函數拋出InvalidOperationException - 顯然是在代碼生成過程中:
無法生成臨時類(result = 1)。
錯誤CS0030:無法將類型'DeleteMe.WeeklyJobsDailyJobsJobsJob []'轉換為'DeleteMe.WeeklyJobsDailyJobsJobsJob'
錯誤CS0029:無法將類型'DeleteMe.WeeklyJobsDailyJobsJobsJob'隱式轉換為'DeleteMe.WeeklyJobsDailyJobsJobsJob []'
(構造函數也拋出了一個FileNotFound異常,但它也會處理它,所以你可以忽略它。)
似乎問題是鋸齒狀數組 - 你是否將1維數組更改為鋸齒狀數組? 當然,錯誤消息指出了我的方向,但我也從未在xsd生成的代碼中看到過鋸齒狀數組,因此看起來很可疑。 我將WeeklyJobsDailyJobsJobsJob[][]
兩次出現更改為WeeklyJobsDailyJobsJobsJob[]
並且應用程序工作得很好。
另外,您應該使用DateTime
作為日期,但XML序列化程序不支持可變日期格式(請參閱https://stackoverflow.com/a/1118855/385844 )。 如果您無法控制源,那么在將字符串存儲到數據庫或任何地方之前,最好將字符串轉換為日期。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.