2017-10-10 56 views
0

我需要反序列化使用C#的嵌套FormXml文件。問題是我不知道如何創建一個包含我需要的所有嵌套元素和屬性的類。動態CRM反序列化嵌套的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> - ><cell> - ><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的班?

回答

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; 
     } 
    } 
} 
+0

它工作的小xml,但不工作在我的情況。(從.xsd創建.cs文件並運行該程序後,我花了很多錯誤( – Renar