使用数组名称包装序列化数组元素

时间:2009-08-04 14:38:44

标签: c# .net xml visual-studio-2008 xml-serialization

我使用的是VSTS2008 + C#+ .Net 3.0。我使用下面的代码来序列化XML,我的对象包含数组类型属性,我想在下面的预期结果中添加一个额外的元素'layer(“MyInnerObjectProperties”元素层),我想将“MyInnerObjectProperties”元素作为父元素所有MyInnerObjectProperty元素的元素)。有什么想法吗?

当前序列化的XML,

<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyObjectProperty>
    <MyInnerObjectProperty>
      <ObjectName>Foo Type</ObjectName>
    </MyInnerObjectProperty>
    <MyInnerObjectProperty>
      <ObjectName>Goo Type</ObjectName>
    </MyInnerObjectProperty>
  </MyObjectProperty>
</MyClass>

预期的序列化XML,

<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyObjectProperty>
    <MyInnerObjectProperties>
      <MyInnerObjectProperty>
        <ObjectName>Foo Type</ObjectName>
      </MyInnerObjectProperty>
      <MyInnerObjectProperty>
        <ObjectName>Goo Type</ObjectName>
      </MyInnerObjectProperty>
    </MyInnerObjectProperties>
  </MyObjectProperty>
</MyClass>

当前代码,

public class MyClass
{
    private MyObject[] _myObjectProperty;

    [XmlElement(IsNullable=false)]
    public MyObject[] MyObjectProperty
    {
        get
        {
            return _myObjectProperty;
        }
        set
        {
            _myObjectProperty = value;
        }
    }
}
public class MyObject
{
    private MyInnerObject[] _myInnerObjectProperty;

    [XmlElement(IsNullable = false)]
    public MyInnerObject[] MyInnerObjectProperty
    {
        get
        {
            return _myInnerObjectProperty;
        }
        set
        {
            _myInnerObjectProperty = value;
        }
    }
}

public class MyInnerObject
{
    public string ObjectName;
}

public class Program
{
    static void Main(string[] args)
    {
        XmlSerializer s = new XmlSerializer(typeof(MyClass));
        FileStream fs = new FileStream("foo.xml", FileMode.Create);
        MyClass instance = new MyClass();
        instance.MyObjectProperty = new MyObject[1];
        instance.MyObjectProperty[0] = new MyObject();
        instance.MyObjectProperty[0].MyInnerObjectProperty = new MyInnerObject[2];
        instance.MyObjectProperty[0].MyInnerObjectProperty[0] = new MyInnerObject();
        instance.MyObjectProperty[0].MyInnerObjectProperty[0].ObjectName = "Foo Type";
        instance.MyObjectProperty[0].MyInnerObjectProperty[1] = new MyInnerObject();
        instance.MyObjectProperty[0].MyInnerObjectProperty[1].ObjectName = "Goo Type";

        s.Serialize(fs, instance);

        return;
    }
}

2 个答案:

答案 0 :(得分:5)

使用XmlArrayItemAttribute

[XmlArray("MyInnerObjectProperties")]     
[XmlArrayItemAttribute("MyInnerObjectProperty", typeof(MyInnerObject),  IsNullable = false)]
public MyInnerObject[] MyInnerObjectProperty
{
   get
     {
         return _myInnerObjectProperty;
     }
   set
     {
        _myInnerObjectProperty = value;
     }
}

答案 1 :(得分:2)

    ...
    [XmlArray(IsNullable = false)]
    [XmlArrayItem("MyInnerObjectProperties")]
    public MyObject[] MyObjectProperty
    {
        get
        {
            return _myObjectProperty;
        }
        set
        {
            _myObjectProperty = value;
        }
    }
    ...