【发布时间】:2021-12-06 05:10:53
【问题描述】:
又是我。
我目前正忙于反序列化特定的 XML 文件,这让我很头疼。
需要反序列化的XML:
<AuditEntryData>
<AuditEntryValue name="Supplier name" newValue="Assagne Corp." type="Text" />
<AuditEntryValue name="MODDATETIME" oldValue="2021-09-15T17:07:37.0000000Z" newValue="2021-09-15T17:29:27.0000000Z" type="DateTime" />
<AuditEntryTableValues>
<T name="InvoiceItems">
<Columns>
<C name="Name" type="Text" />
<C name="Accounting-Key" type="Text" />
<C name="AdditionalColumn" type="Text" />
<C name="Date" type="Date" />
<C name="Value" type="Number" />
</Columns>
<OldValues>
<R>
<C>Oh Long Johnson</C>
<C>23ufztg3u4ghf</C>
<C>Test33</C>
<C>2021-02-10T00:00:00.0000000Z</C>
<C>18.73</C>
</R>
</OldValues>
<NewValues>
<R>
<C>John Smith</C>
<C>23ufztg3u4ghf</C>
<C>Test33</C>
<C>2021-02-10T00:00:00.0000000Z</C>
<C>18.73</C>
</R>
<R>
<C>Johnathan Smith</C>
<C>eui5dctmcotgu</C>
<C>Test22</C>
<C>2021-09-15T00:00:00.0000000Z</C>
<C>13.12</C>
</R>
</NewValues>
</T>
</AuditEntryTableValues>
</AuditEntryData>
用xsd生成的类文件:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[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 C {
private string nameField;
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type {
get {
return this.typeField;
}
set {
this.typeField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[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 R {
private C[] cField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("C")]
public C[] C {
get {
return this.cField;
}
set {
this.cField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[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 AuditEntryData {
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("AuditEntryTableValues", typeof(AuditEntryDataAuditEntryTableValues), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("AuditEntryValue", typeof(AuditEntryDataAuditEntryValue), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("C", typeof(C))]
[System.Xml.Serialization.XmlElementAttribute("R", typeof(R))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class AuditEntryDataAuditEntryTableValues {
private AuditEntryDataAuditEntryTableValuesT[] tField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("T", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public AuditEntryDataAuditEntryTableValuesT[] T {
get {
return this.tField;
}
set {
this.tField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class AuditEntryDataAuditEntryTableValuesT {
private C[][] columnsField;
private C[][][] oldValuesField;
private C[][][] newValuesField;
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("C", typeof(C), IsNullable=false)]
public C[][] Columns {
get {
return this.columnsField;
}
set {
this.columnsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("R", typeof(C[]), IsNullable=false)]
[System.Xml.Serialization.XmlArrayItemAttribute("C", typeof(C), IsNullable=false, NestingLevel=1)]
public C[][][] OldValues {
get {
return this.oldValuesField;
}
set {
this.oldValuesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("R", typeof(C[]), IsNullable=false)]
[System.Xml.Serialization.XmlArrayItemAttribute("C", typeof(C), IsNullable=false, NestingLevel=1)]
public C[][][] NewValues {
get {
return this.newValuesField;
}
set {
this.newValuesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class AuditEntryDataAuditEntryValue {
private string nameField;
private string newValueField;
private string typeField;
private string oldValueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string newValue {
get {
return this.newValueField;
}
set {
this.newValueField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type {
get {
return this.typeField;
}
set {
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string oldValue {
get {
return this.oldValueField;
}
set {
this.oldValueField = value;
}
}
}
反序列化 xml 给我以下错误:
多维数组:
Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'C[]' to 'C'
error CS0030: Cannot convert type 'C[][]' to 'C[]'
error CS0029: Cannot implicitly convert type 'C' to 'C[]'
error CS0029: Cannot implicitly convert type 'C[]' to 'C[][]
一维数组(修改类文件后):
Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'C' to 'C[]'
error CS0029: Cannot implicitly convert type 'C[]' to 'C'
xml 是由外部程序生成的,所以我无法控制它。因此无法更改格式。 如果我删除/注释掉类型属性声明,我可以加载 XML,但它不会加载 OldValues 和 NewValues。
有人能指出正确的方向吗?
【问题讨论】:
标签: c# xml deserialization