如何将复杂的XML转换为.NET类?

我有这个XML ,只是想知道如何转换成C#类

    Sample text...     clientLogo.png    

尝试使用Visual Studio附带的XSD.exe工具。 以下是一些文档: http : //www.codeproject.com/Articles/11317/From-XML-to-Strong-Types-in-C

我建议使用XmlSerializer进行 XML序列化。 基本上,您需要创建与XML结构相对应的类,XmlSerializer负责其余的工作。 如果您可以控制XML格式,最好首先创建类,然后通过XmlSerializer生成一个示例xml,您可以填充实际数据。

Microsoft提供此免费工具,用于从架构生成类。

将类实例转换为Xml,反之亦然,称为序列化/反序列化。 你会在互联网上找到很多关于这个主题的文章,这是一个很好的开始。

最好的解决方案就是这个:

http://www.codeproject.com/Articles/11317/From-XML-to-Strong-Types-in-C

  1. 编写XML结构(XML)
  2. 从XML文件创建XSD文件
  3. 从XSD文件创建C#类

XML

     Sample text...     clientLogo.png    

XSD

                            

C#类

 //------------------------------------------------------------------------------ //  // This code was generated by a tool. // Runtime Version:2.0.50727.5448 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. //  //------------------------------------------------------------------------------ using System.Xml.Serialization; // // This source code was auto-generated by xsd, Version=2.0.50727.3038. // ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] [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 TextScrollerItems { private TextScrollerItemsItem[] itemsField; ///  [System.Xml.Serialization.XmlElementAttribute("Item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public TextScrollerItemsItem[] Items { get { return this.itemsField; } set { this.itemsField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class TextScrollerItemsItem { private string imageField; private TextScrollerItemsItemText[] textField; private string typeField; private string idField; ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string Image { get { return this.imageField; } set { this.imageField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute("Text", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] public TextScrollerItemsItemText[] Text { get { return this.textField; } set { this.textField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string type { get { return this.typeField; } set { this.typeField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string ID { get { return this.idField; } set { this.idField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class TextScrollerItemsItemText { private string colorField; private string valueField; ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string Color { get { return this.colorField; } set { this.colorField = value; } } ///  [System.Xml.Serialization.XmlTextAttribute()] public string Value { get { return this.valueField; } set { this.valueField = value; } } }