views:

455

answers:

2

I am using VSTS2008 + C# + .Net 3.0. I am using below code to serialize XML, and my object contains array type property, but there some additional elements' layer (in my sample, MyInnerObject and MyObject) generated which I want to remove from the generated XML file. Any ideas?

Current generated XML file,

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

Expected XML file,

<?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>
  </MyObjectProperty>
</MyClass>

Current code,

public class MyClass
{
    private MyObject[] _myObjectProperty;

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

    [XmlArrayItemAttribute(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[1];
        instance.MyObjectProperty[0].MyInnerObjectProperty[0] = new MyInnerObject();
        instance.MyObjectProperty[0].MyInnerObjectProperty[0].ObjectName = "Foo Type";
        s.Serialize(fs, instance);

        return;
    }
}
+1  A: 

Instead of

[XmlArrayItemAttribute]

use:

[XmlElement]

To figure this out in the future, you can run (from a VS Command Prompt):

xsd.exe test.xml
xsd.exe /classes test.xsd

This generates test.cs, that contains the xml serializable class, based on the xml. This works even better if you have an .xsd around ofcourse

Sander Rijken
Thanks Sander, your solution works. Could you describe a little more about why using XmlArrayItemAttribute impacts the result XML? And why XmlElement works?
George2
There's just a difference in them, that allows you to use both xml representations, XmlElement skips the property name and just outputs elements, XmlArrayItem creates an array (named with the propertyname) of elements (named with the name you supply)
Sander Rijken
Thanks Sander, question answered. I have a further question here, appreciate if you could help,http://stackoverflow.com/questions/1227897/adding-an-additional-layer-to-xml-serialization-result-for-an-array
George2
I already took a look at that question, I think the answers there are right, can't really improve them.
Sander Rijken
A: 

Is this not basically the same as your existing question?

MattH
No, previous question has nothing to do with array. :-)
George2
No, this has nothing to do with adding extra levels. It is just about serializing arrays with and without a parent container (the MyInnerObjectProperty)
Sander Rijken
Agree with Sander. I personally prefer to separate different topics and avoid a big general topic like issues XML serialization. People advises me before. :-)
George2