某些xml标签未反序列化

时间:2017-08-24 17:33:25

标签: c# xml serialization deserialization

所以我用xsd.exe生成了这个类,当我将XML反序列化为我的对象时,它不会读取所有属性。看起来树中的最后一个属性不会被读取。

这是对象类之一:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(IsNullable=true)]
public partial class relation : myXml {

/// <remarks/>
public object a;

/// <remarks/>
public object b;

/// <remarks/>
public object c;

/// <remarks/>
public object d;

/// <remarks/>
public object f;

/// <remarks/>
public object g;

/// <remarks/>
public object h;

/// <remarks/>
public object i;

/// <remarks/>
public object j;
}

示例XML:

<z id="Fo">
    <attributes>
        <Relation>1<Relation>
        <YVersion>16<YVersion>
        <W>
            <a>1</a>
            <b>2</b>
            <c>3</c>
            <d>4</d>
        </W>
    </attributes>
</z>

当我调试时,具有所有反序列化XML的对象具有所有节点,直到节点W包括节点W及其参数,但是以下节点不以某种方式反序列化,在这种情况下节点A,B,C和D.

此致

雨果

1 个答案:

答案 0 :(得分:0)

听起来W是一种复杂的类型,而Relation&amp; YVersion只是整数。您需要在属性对象上使用默认构造函数来实例化任何复杂类型。此外,您希望序列化的任何其他类型具有复杂类型作为属性需要在默认构造函数中实例化,以便序列化程序能够使用它们。

例如,你的属性类看起来像这样:

[Serializable]
public class Attributes
{
  public int Relation {get; set;}

public int YVersion {get; set;}

  public MyType W {get; set;}

  public Attributes()
  {
     W = new MyType();
  }

}

请记住,您无法将任何参数传递给构造函数,因为序列化程序不会传递任何对象。