9

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">
  <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">
  <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 Answer 1

16

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Sander, your solution works. Could you describe a little more about why using XmlArrayItemAttribute impacts the result XML? And why XmlElement works?
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)
Thanks Sander, question answered. I have a further question here, appreciate if you could help, stackoverflow.com/questions/1227897/…
I already took a look at that question, I think the answers there are right, can't really improve them.
I know that I am WAY late to the party, but this answer helped me out IMMENSELY! So thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.