通过在C#中跳过父级来反序列化XML元素的值

时间:2014-07-04 08:47:01

标签: c# serialization deserialization xmlserializer

假设我有一个XML文件的一部分,其矩阵布局与此类似:

XML中的矩阵

<matrix>
 <row1>
   <col1>1.00</col1>
   <col2>0.00</col2>
   <col3>1.00</col3>
   <col4>1.00</col4>
 </row1>
 <row2>
   <col1>0.00</col1>
   <col2>1.00</col2>
   <col3>1.00</col3>
   <col4>0.00</col4>
 </row2>
</matrix>

并说id最终得到这样的结构:

所需的结构

public struct sMatrix
{
    public float m11;
    public float m12;
    public float m13;
    ...
    public float m23;
    public float m24;
}

我应该如何声明这个结构能够通过XmlSerializer反序列化?是否有一些Xml属性我可以给结构中的浮点数告诉他们,例如,m11是row1的子节点?我试过这个:[XmlElement("matrix/row1/col1")但它没有用。

1 个答案:

答案 0 :(得分:0)

好吧,您可以将矩阵结构的所有字段标记为不可序列化,并引入“行”结构来序列化和反序列化您的行,如下所示:

[Serializable]
public struct sMatrixRow
{
    public sMatrixRow(float col1, float col2, float col3, float col4)
    {
        this.col1 = col1;
        this.col2 = col2;
        this.col3 = col3;
        this.col4 = col4;
    }
    public float col1;
    public float col2;
    public float col3;
    public float col4;
}

[Serializable]
public struct sMatrix
{
    [XmlElement("row1")]
    public sMatrixRow Row1
    {
        get
        {
            return new sMatrixRow(m00, m01, m02, m03);
        }
        set
        {
            this.m00 = value.col1;
            this.m01 = value.col2;
            this.m02 = value.col3;
            this.m03 = value.col4;
        }
    }

    // And so on, for Rows 2 through 4

    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m00;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m01;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m02;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m03;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m10;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m11;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m12;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m13;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m20;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m21;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m22;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m23;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m30;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m31;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m32;
    [System.NonSerialized, System.Xml.Serialization.XmlIgnore]
    public float m33;
}

更新:此方案成功地序列化和反序列化包含类中的以下“矩阵”字段:

  <matrix>
    <row1>
      <col1>2</col1>
      <col2>0</col2>
      <col3>0</col3>
      <col4>0</col4>
    </row1>
    <row2>
      <col1>0</col1>
      <col2>2</col2>
      <col3>0</col3>
      <col4>0</col4>
    </row2>
    <row3>
      <col1>0</col1>
      <col2>0</col2>
      <col3>2</col3>
      <col4>0</col4>
    </row3>
    <row4>
      <col1>0</col1>
      <col2>0</col2>
      <col3>0</col3>
      <col4>1</col4>
    </row4>
  </matrix>

这就是你想要的吗?

更新:将[XmlElement(“row1”)]添加到Row1(对于其他行也类似),以使行名称缩写。我在这里基本上做的是使用可序列化属性来创建数据的XML可见表示,这些表示与底层字段不同但是同构。