如何反序列化这个Xml文件?

时间:2011-02-25 15:11:33

标签: c# serialization

我有这个Xml文件,我需要将它反序列化回一个类。问题是:考虑到Xml元素(RowInfo)的计数不是常数,这个类的正确结构/设计是什么? 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>

感谢您的帮助。 感谢。

Edit1 :还考虑到(图层)可能包含多个图层。

6 个答案:

答案 0 :(得分: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;}
}

答案 1 :(得分: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
}

唯一的区别是编码,但你应该可以解决它。

答案 2 :(得分:1)

如果RowInfo的计数不是常数,请在班上使用List

答案 3 :(得分: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() ;

答案 4 :(得分:1)

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

您可以使用以下代码XmlSerializer并调用反序列化:)

//------------------------------------------------------------------------------
// <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;
        }
    }
}

答案 5 :(得分:0)

签出一个名为XSD.exe的工具,它随Visual Studio一起提供,允许您从xml文件生成代码。

您应该在visual studio旁边的程序文件菜单中看到它。