2011-02-25 44 views
4

我有這個Xml文件,我需要反序列化它回到一個類。問題是:考慮到Xml元素(RowInfo)的計數不是恆定的,這個類的正確結構/設計是什麼? The Xml文件:如何反序列化這個Xml文件?

<?xml version="1.0"?> 
<SomeObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" > 
    <Layers> 
<Layer Id="0"> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1</RowInfo> 
     <RowInfo>1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> 
     <RowInfo>1,1,1,0,0,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1</RowInfo> 
     <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1</RowInfo> 
    </Layer> 
    </Layers> 
</SomeObject> 

感謝您的幫助。 謝謝。

編輯1:也考慮到(層)可能包含多個圖層。

+0

我想將(Layer)元素作爲數組獲取,但是,我不知道如何構造該類:( – 2011-02-25 15:21:12

+0

)您可能將Layer元素作爲私有List,然後擁有一個公共屬性通過ToArray()將列表轉換爲數組( – Phil 2011-02-25 15:31:03

回答

2

這應該工作,只要你想:

public class SomeObject 
{ 
    public List<Layer> Layers { get; set; } 
} 

public class Layer 
{ 
    [XmlAttribute] 
    public int Id { get; set; } 

    [XmlElement("RowInfo")] 
    public List<RowInfo> RowInfos { get; set; } 
} 

public class RowInfo 
{ 
    [XmlText] 
    public string Info { get; set; } // you'll need to parse the list of ints manually 
} 

唯一不同的是編碼,但你應該能夠解決它。

+0

謝謝,簡單幹淨,它工作正常。我可以接受這個答案。並感謝大家在這裏幫助:) – 2011-02-25 16:07:05

1

如果RowInfo的計數不固定,請在班級中使用List

1

我會說使用LINQ-to-Xml。讓你的對象的構造函數,可以採取xlement然後做沿着

public class YourObject() 
{ 
public IEnumerable<Layer> Layers { get; set; } 
public int Id { get; set; } 

    public YourObj(XElement x) 
    { 
     this.Id = int.Parse(x.Attribute("Id").ToString()); 
     this.Layers = from layer in x.Elements("Layer") 
        select new Layer(layer); 
    } 
} 



var objs = (from c in XElement.Load("your.xml").Elements("layer") 
      select new YourObject(c)).ToList() ; 
3

類似下面的行 東西 - 名稱將需要改變,以保護無辜:

類結構之後評論

public class SomeObject 
{ 
    public List<Layer> Layers {get;set;} 
} 

public class Layer 
{ 
    public int Id {get;set;} 
    public List<RowInfo> RowInfos {get;set;} 
} 

public class RowInfo 
{ 
    public List<Row> Rows {get;set;} 
} 

public class Row 
{ 
    public int RowData {get;set;} 
} 
+0

)謝謝,但是,(ID)屬性屬於(Layer)元素。這可以工作嗎? – 2011-02-25 15:26:44

+0

掛起,我將進行編輯 - 不要認爲這很難修復。 – amelvin 2011-02-25 15:49:56

+0

+1謝謝你的時間? – 2011-02-25 16:12:39

1
XmlSerializer serializer = new XmlSerializer(typeof(SomeObject)); 

可以使用XmlSerializer與跟隨着G代碼和反序列化調用:)

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:2.0.50727.4952 
// 
//  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 SomeObject { 

    private SomeObjectLayers layersField; 

    /// <remarks/> 
    public SomeObjectLayers Layers { 
     get { 
      return this.layersField; 
     } 
     set { 
      this.layersField = 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 SomeObjectLayers { 

    private SomeObjectLayersLayer layerField; 

    /// <remarks/> 
    public SomeObjectLayersLayer Layer { 
     get { 
      return this.layerField; 
     } 
     set { 
      this.layerField = 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 SomeObjectLayersLayer { 

    private decimal[] rowInfoField; 

    private int idField; 

    private bool idFieldSpecified; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("RowInfo")] 
    public decimal[] RowInfo { 
     get { 
      return this.rowInfoField; 
     } 
     set { 
      this.rowInfoField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public int Id { 
     get { 
      return this.idField; 
     } 
     set { 
      this.idField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlIgnoreAttribute()] 
    public bool IdSpecified { 
     get { 
      return this.idFieldSpecified; 
     } 
     set { 
      this.idFieldSpecified = value; 
     } 
    } 
} 
+0

謝謝,我需要嘗試這個。我看到這是一個自動生成的代碼,這是什麼工具? – 2011-02-25 15:47:02

+0

我創建了一個通用模式基於xml並使用[xsd.exe /classes](http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.71).aspx)生成類。 – 2011-02-25 15:48:36

+0

+1這是非常有用的answeranswer – 2011-02-25 16:07:59

0

簽出一個叫做XSD.exe的工具,它帶有visual studio,可以讓你從xml文件生成代碼。

您應該在visual studio旁邊的程序文件菜單上看到它。