2012-02-13 61 views
2

我有這個XML只是想知道如何我可以轉換成C#類如何將複雜的XML轉換爲.NET類?

<?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> 
+2

你必須清楚地定義你的XML **什麼都可以* *。項目小孩是否總是改變?它改變了什麼? – gideon 2012-02-13 12:05:17

+0

只搜索'xml序列化' – 2012-02-13 12:06:04

+1

看到這個主題,看方法2轉換XML 2 CS沒有XSD: http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document – 2015-05-12 08:50:56

回答

6

我推薦使用XmlSerializer來進行XML序列化。基本上,您需要創建對應於XML結構的類,並且XmlSerializer負責其餘部分。 如果您可以控制XML格式,最好先創建類,然後通過XmlSerializer生成一個示例xml,然後用實數填充。

2

將類實例轉換爲Xml,反過來稱爲序列化/反序列化。你會在網上找到很多關於該主題的文章,這是一個好開始here

3

Microsoft提供this免費工具,用於從模式生成類。

0

最好的解決方案,這是一個:

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

  1. 寫XML的結構(XML)
  2. 從XML文件中創建XSD文件
  3. 創建C#來自XSD文件的課程

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; 
     } 
    } 
} 
相關問題