我在這裏有關於反序列化的問題。我有一個XML文件,我需要將它反序列化到從Service引用中獲得的類中。我知道如何反序列化一個XML文件,但是當我嘗試反序列化這個文件時,我得到一個空的類對象。我不明白爲什麼這樣做。C#中的XML反序列化返回一個空對象
的XML文件中的內容是這樣的:
<?xml version="1.0" encoding="UTF-8" ?><iVAZFile xmlns="http://www.v.lt/c/i/iv">
<FileDescription>
<FileVersion>i1.3.3</FileVersion>
<FileDateCreated>2016-11-07T12:28:32</FileDateCreated>
<SoftwareCompanyName>otechnika"</SoftwareCompanyName>
<SoftwareName>Eita</SoftwareName>
<SoftwareVersion>2016.9</SoftwareVersion>
<CreatorRegistrationNumber>123060356</CreatorRegistrationNumber>
</FileDescription>
</iVAZFile>
類看起來是這樣的:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1590.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.v.lt/c/i/iv")]
public partial class FileDescription : object, System.ComponentModel.INotifyPropertyChanged {
private string fileVersionField;
private System.DateTime fileDateCreatedField;
private string softwareCompanyNameField;
private string softwareNameField;
private string softwareVersionField;
private ulong creatorRegistrationNumberField;
public FileDescription() {
this.fileVersionField = "iVAZ1.3.3";
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public string FileVersion {
get {
return this.fileVersionField;
}
set {
this.fileVersionField = value;
this.RaisePropertyChanged("FileVersion");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public System.DateTime FileDateCreated {
get {
return this.fileDateCreatedField;
}
set {
this.fileDateCreatedField = value;
this.RaisePropertyChanged("FileDateCreated");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=2)]
public string SoftwareCompanyName {
get {
return this.softwareCompanyNameField;
}
set {
this.softwareCompanyNameField = value;
this.RaisePropertyChanged("SoftwareCompanyName");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=3)]
public string SoftwareName {
get {
return this.softwareNameField;
}
set {
this.softwareNameField = value;
this.RaisePropertyChanged("SoftwareName");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=4)]
public string SoftwareVersion {
get {
return this.softwareVersionField;
}
set {
this.softwareVersionField = value;
this.RaisePropertyChanged("SoftwareVersion");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=5)]
public ulong CreatorRegistrationNumber {
get {
return this.creatorRegistrationNumberField;
}
set {
this.creatorRegistrationNumberField = value;
this.RaisePropertyChanged("CreatorRegistrationNumber");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
而且我使用填充類中的方法:
using (MemoryStream memoryStream = new MemoryStream())
{
using (BinaryWriter binWriter = new BinaryWriter(memoryStream, Encoding.UTF8))
{
string filep = File.ReadAllText("test.xml");
binWriter.Write(filep);
memoryStream.Seek(2, SeekOrigin.Begin);
using (StreamReader streamReader = new StreamReader(memoryStream))
{
using (XmlReader reader = XmlReader.Create(streamReader))
{
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "FileDescription";
xRoot.IsNullable = false;
XmlSerializer serializer = new XmlSerializer(typeof(FileDescription),xRoot);
FileDescription dsc = new FileDescription();
dsc=(FileDescription)serializer.Deserialize(reader);
}
}
}
}
一些額外的評論說我也試過:
- 我做了我自己的班級,看起來與服務參考中的一樣,並添加了一個新參數
[XmlRoot(ElementName="FileDescription"),XmlType("FileDescription")]
然後在我的班級上進行了deserilization。 - 嘗試用手工添加數值而不使用可以正常工作的值,所以服務代碼沒有問題。
- 另外,當我反序列化的XML我刪除
<iVAZFile xmlns="http://www.v.lt/c/i/iv">
和</iVAZFIle>
因爲當反序列化它會拋出異常並不期待。
最後幾個字我不能改變服務引用類,我需要使用該類以備後用,所以我不能做我自己的。
反向操作 - 創建一個對象並對其進行序列化。你會更容易發現差異在哪裏。對於我來說,你試圖使用需要'FileDescription'的某個'iVAZFile'元素進行反序列化。 –
是的,我忘了補充一點,我也刪除和 ,因爲當反序列化它會拋出異常「不期待
您是否將名稱空間('xmlns'屬性)添加回到'FileDescription'?如果不是,他們將不匹配。 –