我需要使用C#反序列化嵌套的FormXml文件。问题是我不知道如何创建一个包含所有我需要的嵌套元素和属性的类。
FormXml如下:(我使xml更简单)
<form >
<tabs>
<tab name="tab_1" ... >
<labels>
<label description="Tab1" languagecode="1049" />
</labels>
<columns>
<column width="100%">
<sections>
<section name="ACCOUNT_INFORMATION" .. >
<labels>
<label description="About" languagecode="1049" />
</labels>
<rows>
<row>
<cell id="{}">
<labels>
<label description="Phone" languagecode="1049" />
</labels>
<control id="telephone1"/>
</cell>
</row>
<row>
<!--same -->
</row>
</rows>
</section>
<section name="COMPANY_PROFILE" .. >
</section>
</sections>
</column>
</columns>
</tab>
<tab name="tab_2" ... >
<!--same -->
</tab>
</tabs>
</form>
我需要在<section>
<section>
和<labels>
中使用<rows>
数组:
在<labels>
中,点击<label>
属性< .. descrition = "..">
在<rows>
中,点击<row>
- &gt; <cell>
- &gt; <control id>
我创建了类XmlParser:
public class SectionRoot
{
[XmlArray("section", Form = XmlSchemaForm.Unqualified)]
[XmlArrayItem("labels", Form = XmlSchemaForm.Unqualified)]
public Section[] Sections { get; set; }
}
[XmlRoot("label")]
public class Section
{
[XmlAttribute("description")]
public string SectionName { get; set; }
}
[XmlRoot("row")]
public class Row
{
[XmlElement("cell")]
public Control Cell { get; set; }
}
[XmlRoot("control")]
public class Control
{
[XmlAttribute("id")]
public string Attribute { get; set; }
}
但我很困惑..如何制作真正的XmlParser类?
答案 0 :(得分:1)
您可以使用xsd.exe工具。首先,从xml代码创建一个xsd模式。将xml保存在名为form.xml
的文件中xsd.exe form.xml
现在,从生成的模式中创建一个类:
xsd.exe form.xsd /c
这是我得到的输出。
//------------------------------------------------------------------------------
// <auto-generated>
// Este código fue generado por una herramienta.
// Versión de runtime:4.0.30319.42000
//
// Los cambios en este archivo podrían causar un comportamiento incorrecto y se perderán si
// se vuelve a generar el código.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// Este código fuente fue generado automáticamente por xsd, Versión=4.6.1055.0.
//
/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.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 labels {
private labelsLabel[] labelField;
/// <comentarios/>
[System.Xml.Serialization.XmlElementAttribute("label", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public labelsLabel[] label {
get {
return this.labelField;
}
set {
this.labelField = value;
}
}
}
/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class labelsLabel {
private string descriptionField;
private string languagecodeField;
/// <comentarios/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string description {
get {
return this.descriptionField;
}
set {
this.descriptionField = value;
}
}
/// <comentarios/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string languagecode {
get {
return this.languagecodeField;
}
set {
this.languagecodeField = value;
}
}
}
/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.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 form {
private object[] itemsField;
/// <comentarios/>
[System.Xml.Serialization.XmlElementAttribute("labels", typeof(labels))]
[System.Xml.Serialization.XmlElementAttribute("tabs", typeof(formTabs), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class formTabs {
private formTabsTab[] tabField;
/// <comentarios/>
[System.Xml.Serialization.XmlElementAttribute("tab", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public formTabsTab[] tab {
get {
return this.tabField;
}
set {
this.tabField = value;
}
}
}
/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class formTabsTab {
private labelsLabel[][] labelsField;
private formTabsTabColumnsColumn[][] columnsField;
private string nameField;
/// <comentarios/>
[System.Xml.Serialization.XmlArrayItemAttribute("label", typeof(labelsLabel), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public labelsLabel[][] labels {
get {
return this.labelsField;
}
set {
this.labelsField = value;
}
}
/// <comentarios/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("column", typeof(formTabsTabColumnsColumn), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public formTabsTabColumnsColumn[][] columns {
get {
return this.columnsField;
}
set {
this.columnsField = value;
}
}
/// <comentarios/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
}
/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class formTabsTabColumnsColumn {
private formTabsTabColumnsColumnSectionsSection[][] sectionsField;
private string widthField;
/// <comentarios/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("section", typeof(formTabsTabColumnsColumnSectionsSection), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public formTabsTabColumnsColumnSectionsSection[][] sections {
get {
return this.sectionsField;
}
set {
this.sectionsField = value;
}
}
/// <comentarios/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string width {
get {
return this.widthField;
}
set {
this.widthField = value;
}
}
}
/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class formTabsTabColumnsColumnSectionsSection {
private labelsLabel[][] labelsField;
private formTabsTabColumnsColumnSectionsSectionRowsRowCell[][][] rowsField;
private string nameField;
/// <comentarios/>
[System.Xml.Serialization.XmlArrayItemAttribute("label", typeof(labelsLabel), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public labelsLabel[][] labels {
get {
return this.labelsField;
}
set {
this.labelsField = value;
}
}
/// <comentarios/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("row", typeof(formTabsTabColumnsColumnSectionsSectionRowsRowCell[]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
[System.Xml.Serialization.XmlArrayItemAttribute("cell", typeof(formTabsTabColumnsColumnSectionsSectionRowsRowCell), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false, NestingLevel=1)]
public formTabsTabColumnsColumnSectionsSectionRowsRowCell[][][] rows {
get {
return this.rowsField;
}
set {
this.rowsField = value;
}
}
/// <comentarios/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
}
/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class formTabsTabColumnsColumnSectionsSectionRowsRowCell {
private labelsLabel[][] labelsField;
private formTabsTabColumnsColumnSectionsSectionRowsRowCellControl[] controlField;
private string idField;
/// <comentarios/>
[System.Xml.Serialization.XmlArrayItemAttribute("label", typeof(labelsLabel), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public labelsLabel[][] labels {
get {
return this.labelsField;
}
set {
this.labelsField = value;
}
}
/// <comentarios/>
[System.Xml.Serialization.XmlElementAttribute("control", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public formTabsTabColumnsColumnSectionsSectionRowsRowCellControl[] control {
get {
return this.controlField;
}
set {
this.controlField = value;
}
}
/// <comentarios/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
}
/// <comentarios/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class formTabsTabColumnsColumnSectionsSectionRowsRowCellControl {
private string idField;
/// <comentarios/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
}