我已阅读多个线程,并且我能够使用C#的序列化方法反序列化XML。我遇到的问题是我提供的XML不像我以前见过的那样。而不是拥有一个包含项目列表的对象,而是获得一个包含多个属性的项目的对象。这就是我所拥有的。这是XML:
<DATA>
<OBJECT TYPE="Sheet consumption">
<ITEM>
<PROPERTY NAME="Sheet">GN18 48x120</PROPERTY>
<PROPERTY NAME="Sheet used area">31.556835306782627</PROPERTY>
<PROPERTY NAME="Made quantity">2</PROPERTY>
<PROPERTY NAME="Required quantity">2</PROPERTY>
<PROPERTY NAME="Sheet weight">34.132561</PROPERTY>
<PROPERTY NAME="Sheet width">1219.2</PROPERTY>
<PROPERTY NAME="Sheet length">3048</PROPERTY>
<PROPERTY NAME="Sheet thickness">1.1684</PROPERTY>
<PROPERTY NAME="Real time">0.712</PROPERTY>
<PROPERTY NAME="Production order">139020</PROPERTY>
<PROPERTY NAME="Item">W220-09528</PROPERTY>
<PROPERTY NAME="Quantity/Sheet">55</PROPERTY>
</ITEM>
<ITEM>
<PROPERTY NAME="Sheet">GN20 48x120</PROPERTY>
<PROPERTY NAME="Sheet used area">35.895446716901191</PROPERTY>
<PROPERTY NAME="Made quantity">14</PROPERTY>
<PROPERTY NAME="Required quantity">14</PROPERTY>
<PROPERTY NAME="Sheet weight">26.618381</PROPERTY>
<PROPERTY NAME="Sheet width">1219.2</PROPERTY>
<PROPERTY NAME="Sheet length">3048</PROPERTY>
<PROPERTY NAME="Sheet thickness">0.9144</PROPERTY>
<PROPERTY NAME="Real time">0.414</PROPERTY>
<PROPERTY NAME="Production order">139019</PROPERTY>
<PROPERTY NAME="Item">W220-09533-C</PROPERTY>
<PROPERTY NAME="Quantity/Sheet">7</PROPERTY>
</ITEM>
</OBJECT>
</DATA>
这就是该类反序列化的方式
使用System.Collections.Generic; 使用System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
//
namespace WindowsFormsApplication1
{
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[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 DATA
{
private DATAOBJECT itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElement("OBJECT")]
public DATAOBJECT Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class DATAOBJECT
{
private List<DATAOBJECTITEMPROPERTY> iTEMField;
private string tYPEField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItem("PROPERTY", typeof(DATAOBJECTITEMPROPERTY), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public List<DATAOBJECTITEMPROPERTY> ITEM
{
get
{
return this.iTEMField;
}
set
{
this.iTEMField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string TYPE
{
get
{
return this.tYPEField;
}
set
{
this.tYPEField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DATAOBJECTITEMPROPERTY
{
private string nAMEField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string NAME
{
get
{
return this.nAMEField;
}
set
{
this.nAMEField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
}
当我反序列化时,我期望的结果是有2个类型为DATAOBJECTITEMPROPERTY
的对象,而是我得到24个DATAOBJECTITEMPROPERTY
类型的对象,其中基本上是每个属性名称。
我不知道我做错了什么。
答案 0 :(得分:0)
你做错了什么,你用来生成复杂事物的类的工具,省略了一个级别。我使用像http://xmltocsharp.azurewebsites.net/这样的在线生成器来为我生成类别。
它为您的示例生成了更清晰,更易于理解的类。
[XmlRoot(ElementName = "PROPERTY")]
public class PROPERTY
{
[XmlAttribute(AttributeName = "NAME")]
public string NAME { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "ITEM")]
public class ITEM
{
[XmlElement(ElementName = "PROPERTY")]
public List<PROPERTY> PROPERTY { get; set; }
}
[XmlRoot(ElementName = "OBJECT")]
public class OBJECT
{
[XmlElement(ElementName = "ITEM")]
public List<ITEM> ITEM { get; set; }
[XmlAttribute(AttributeName = "TYPE")]
public string TYPE { get; set; }
}
[XmlRoot(ElementName = "DATA")]
public class DATA
{
[XmlElement(ElementName = "OBJECT")]
public List<OBJECT> OBJECT { get; set; }
}