【问题标题】:Dynamics CRM Deserialize nested FormXmlDynamics CRM 反序列化嵌套的 FormXml
【发布时间】:2017-10-10 07:55:20
【问题描述】:

我需要使用 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>

我需要在&lt;section&gt; &lt;labels&gt;&lt;rows&gt; 中获取&lt;section&gt; 数组:

&lt;labels&gt; 中取&lt;label&gt; 属性&lt; .. descrition = ".."&gt;

&lt;rows&gt; 中取&lt;row&gt; -> &lt;cell&gt; -> &lt;control id&gt;

我创建了 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 类?

【问题讨论】:

    标签: c# xml serialization xml-parsing crm


    【解决方案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;
            }
        }
    }
    

    【讨论】:

    • 它适用于小型 xml,但不适用于我的情况。(从 .xsd 创建 .cs 文件并运行程序后,我遇到了很多错误(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多