4

I would like to de/serialize a XML-Document with type=array as root node. The given xml structure looks like this:

<?xml version="1.0" encoding="UTF-8"?>
    <parties type="array">
        <party type="Person">
            <id>1</id>
            <lastname>Smith</lastname>
            <firstname>Peter</firstname>
            ...
        </party>
        <party type="Person">
            <id>2</id>
            <lastname>Smith</lastname>
            <firstname>Sarah</firstname>
            ...
        </party>
    <parties type="array">

C# Code looks like this:

[XmlRootAttribute("parties", Namespace = "", IsNullable = false)]       
public class Parties
{
    private ArrayList contacts = new ArrayList();


    public Parties()
    {

    }


    [XmlArray("parties"), XmlArrayItem("party", typeof(Person))]
    public ArrayList Contacts
    {
        get { return contacts; }
        set { contacts = value; }
    }
}

The resulting xml output is this:

<?xml version="1.0" encoding="utf-8"?>
    <parties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <parties>
            <party>
                <id>0</id>
                <lastname>Smith</last-name>
                <firstname>Peter</first-name>
            </party>
        </parties>
    </parties>

The problem is that I have 2 -tags now. How can I specify array type for the root element? Any ideas how to fix it without changing the given xml schema?

1 Answer 1

6

Try this:

[XmlElement("party")]
public ArrayList Contacts
{
    get { return contacts; }
    set { contacts = value; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1; minor point - it only needs a get as long as the value is initialized correctly.
That solved the problem. @peter, consider marking this as the answer if it is the case.

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.