【问题标题】:How to convert complex XML to .NET Class? [duplicate]如何将复杂的 XML 转换为 .NET 类? [复制]
【发布时间】:2012-03-04 20:02:26
【问题描述】:

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

<?xml version="1.0" encoding="utf-8"?>
<TextScrollerItems xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <Item type="text" ID="234">
     <Text Color="Blue">
       Sample text...
     </Text>
    </Item>

    <Item type="image" ID="2456">
        <Image>
          clientLogo.png
        </Image>
    </Item>

    </TextScrollerItems>

【问题讨论】:

  • 您必须清楚地定义您的 xml 可以是什么。项目子项是否总是改变?它变成了什么?
  • 只要搜索xml serialization
  • 看这个话题,看看方法2在没有XSD的情况下转换XML 2 CS:stackoverflow.com/questions/364253/…

标签: c# .net xml class


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        将类实例转换为 Xml 和其他方式称为序列化/反序列化。你会在互联网上找到很多关于该主题的文章,一个好的开始here

        【讨论】:

          【解决方案5】:

          最好的解决方案是这个:

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

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

          XML

          <?xml version="1.0" encoding="utf-8"?>
          <TextScrollerItems xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          
              <Item type="text" ID="234">
               <Text Color="Blue">
                 Sample text...
               </Text>
              </Item>
          
              <Item type="image" ID="2456">
                  <Image>
                    clientLogo.png
                  </Image>
              </Item>
          
              </TextScrollerItems>
          

          XSD

          <?xml version="1.0" encoding="utf-8"?>
          <xs:schema id="TextScrollerItems" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
            <xs:element name="TextScrollerItems" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
              <xs:complexType>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                  <xs:element name="Item">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="Image" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
                        <xs:element name="Text" nillable="true" minOccurs="0" maxOccurs="unbounded">
                          <xs:complexType>
                            <xs:simpleContent msdata:ColumnName="Text_Text" msdata:Ordinal="1">
                              <xs:extension base="xs:string">
                                <xs:attribute name="Color" type="xs:string" />
                              </xs:extension>
                            </xs:simpleContent>
                          </xs:complexType>
                        </xs:element>
                      </xs:sequence>
                      <xs:attribute name="type" type="xs:string" />
                      <xs:attribute name="ID" type="xs:string" />
                    </xs:complexType>
                  </xs:element>
                </xs:choice>
              </xs:complexType>
            </xs:element>
          </xs:schema>
          

          C# 类

          //------------------------------------------------------------------------------
          // <auto-generated>
          //     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.
          // </auto-generated>
          //------------------------------------------------------------------------------
          
          using System.Xml.Serialization;
          
          // 
          // This source code was auto-generated by xsd, Version=2.0.50727.3038.
          // 
          
          
          /// <remarks/>
          [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;
          
              /// <remarks/>
              [System.Xml.Serialization.XmlElementAttribute("Item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
              public TextScrollerItemsItem[] Items {
                  get {
                      return this.itemsField;
                  }
                  set {
                      this.itemsField = value;
                  }
              }
          }
          
          /// <remarks/>
          [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;
          
              /// <remarks/>
              [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
              public string Image {
                  get {
                      return this.imageField;
                  }
                  set {
                      this.imageField = value;
                  }
              }
          
              /// <remarks/>
              [System.Xml.Serialization.XmlElementAttribute("Text", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
              public TextScrollerItemsItemText[] Text {
                  get {
                      return this.textField;
                  }
                  set {
                      this.textField = value;
                  }
              }
          
              /// <remarks/>
              [System.Xml.Serialization.XmlAttributeAttribute()]
              public string type {
                  get {
                      return this.typeField;
                  }
                  set {
                      this.typeField = value;
                  }
              }
          
              /// <remarks/>
              [System.Xml.Serialization.XmlAttributeAttribute()]
              public string ID {
                  get {
                      return this.idField;
                  }
                  set {
                      this.idField = value;
                  }
              }
          }
          
          /// <remarks/>
          [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;
          
              /// <remarks/>
              [System.Xml.Serialization.XmlAttributeAttribute()]
              public string Color {
                  get {
                      return this.colorField;
                  }
                  set {
                      this.colorField = value;
                  }
              }
          
              /// <remarks/>
              [System.Xml.Serialization.XmlTextAttribute()]
              public string Value {
                  get {
                      return this.valueField;
                  }
                  set {
                      this.valueField = value;
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2017-05-18
            • 2020-12-09
            • 1970-01-01
            • 2019-12-11
            • 1970-01-01
            • 1970-01-01
            • 2014-08-07
            • 1970-01-01
            • 2015-02-07
            相关资源
            最近更新 更多