2017-10-10 6 views
0

C#을 사용하여 중첩 된 FormXml 파일을 deserialize해야합니다. 문제는 중첩 된 요소와 속성이 모두 필요한 클래스를 만드는 방법을 알지 못한다는 것입니다.동적 CRM 중첩 된 FormXml 비 직렬화

FormXml은 다음과 같습니다 : (내가 만드는 간단한 XML) <section><labels><rows>에서

<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> 배열을 필요 : <labels>에서

<label> 속성 < .. descrition = "..">

에서를 취할 <rows> 걸릴 <row> -><cell> ->내가 진정한 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 스키마를 만듭니다. 생성 된 스키마에서 클래스를 생성, 이제

xsd.exe form.xml 

form.xml라는 파일에 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

내 경우에는 작지만 작동하지 않습니다. – Renar