views:

193

answers:

2

Hello everyone,

I am using VSTS2008 + C# + .Net 3.0. I am using below code to serialize XML, and my object contains array type property, and I want to add an additional elements' layer ("MyInnerObjectProperties" element layer in my expected results below, and I want to make "MyInnerObjectProperties" element as parent element for all MyInnerObjectProperty element). Any ideas?

Current serialized XML,

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

Expected serialized XML,

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

Current code,

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

thanks in advance, George

+2  A: 

make use of the XmlArrayItemAttribute

[XmlArray("MyInnerObjectProperties")]     
[XmlArrayItemAttribute("MyInnerObjectProperty", typeof(MyInnerObject),  IsNullable = false)]
public MyInnerObject[] MyInnerObjectProperty
{
   get
     {
         return _myInnerObjectProperty;
     }
   set
     {
        _myInnerObjectProperty = value;
     }
}
Stan R.
Your solution works! Cool!
George2
@George2: please look at the updated answer, you wont have to change your properties name, just the declaration. I added XmlArray to rename the element name.
Stan R.
by declaration I mean..the declarative syntax used by the serializer...
Stan R.
Thanks Stan! I have a related question about namespace serializtion issue here, appreciate if you could take a look, http://stackoverflow.com/questions/1228127/xml-serialization-namespace-issue
George2
+2  A: 
    ...
    [XmlArray(IsNullable = false)]
    [XmlArrayItem("MyInnerObjectProperties")]
    public MyObject[] MyObjectProperty
    {
        get
        {
            return _myObjectProperty;
        }
        set
        {
            _myObjectProperty = value;
        }
    }
    ...
Thomas Levesque
this will not produce the desired xml...it will stick MyObject node in there.
Stan R.
This works fine, I checked...
Thomas Levesque
i checked too..this is what i get. <MyClass> <MyObjectProperty> <MyInnerObjectProperties> <MyInnerObjectProperty> <MyInnerObject> <ObjectName>Foo Type</ObjectName> </MyInnerObject> <MyInnerObject> <ObjectName>Goo Type</ObjectName> </MyInnerObject> </MyInnerObjectProperty> </MyInnerObjectProperties> </MyObjectProperty></MyClass> notice <MyInnerObject> .. this is something he did not want. http://stackoverflow.com/questions/1227693/xml-array-serialization-issue-in-c
Stan R.
Are you sure you checked with the same code ? This is not what I get... Note that I apply the attribute to MyObjectProperty, not MyInnerObjectProperty. I get this XML :<MyClass> <MyObjectProperty> <MyInnerObjectProperties> <MyInnerObjectProperty> <ObjectName>Foo Type</ObjectName> </MyInnerObjectProperty> <MyInnerObjectProperty> <ObjectName>Goo Type</ObjectName> </MyInnerObjectProperty> </MyInnerObjectProperties> </MyObjectProperty></MyClass>
Thomas Levesque
strange. i just copied and pasted your code and i still don't get your result.
Stan R.