2

I have this class

public class dtoObject : CommonBase
{
    [XmlArray("SomeItems"), XmlArrayItem("SomeItem")]
    public List<dtoSomeItem> SomeItems
    {
        get { return _SomeItems; }
        set { _SomeItems = value; }
    }
 }

and I would would like to use XML Serialization to make the XML string appear to be:

<Objects>
    <Object>
        <SomeItems>
            <SomeItem>
              1
            </SomeItem>
            <SomeItem>
              2
            </SomeItem>
        </SomeItems>
    </Object>
    <Object>
        <SomeItems>
            <SomeItem>
              3
            </SomeItem>
            <SomeItem>
              4
            </SomeItem>
        </SomeItems>
    </Object>
</Objects>

But for the life of me I cant figure out what to put before

public class dtoObject

in terms of Attributes, so that I get

<Objects><Object>...</Object><Object>...

when I serialize this.

Any ideas?

3 Answers 3

1

I don't think that you can do this with your object structure because the XML effectively declares a wrapper around the SomeItems collection. For example:

[XmlType("Objects")]
public class dtoObject : CommonBase
{
    [XmlElement("Object")]
    public List<dtoSomeItemWrapper> SomeItemsWrappers
    {
        get { return _SomeItemsWrappers; }
        set { _SomeItemsWrappers = value; }
    }
 }

class dtoSomeItemWrapper
{
    [XmlArray("SomeItems"), XmlArrayItem("SomeItem")]
    public List<dtoSomeItem> SomeItems
    {
        get { return _SomeItems; }
        set { _SomeItems = value; }
    }
}   
Sign up to request clarification or add additional context in comments.

2 Comments

comptent_tech I appreciate the reply, but the xml string that gets generated is like this <?xml version="1.0"?><ArrayOfDtoObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><dtoObject><SomeItems><SomeItem></SomeItem></SomeItems></dtoObject><dtoObject><SomeItems /></dtoObject><dtoObject><SomeItems><SomeItem></SomeItem><SomeItem></SomeItem></SomeItems></dtoObject></ArrayOfDtoObject>
@user1066353: Ok, updated the answer so that it should generate exactly what you need.
1

Here is what I got to work. Thanks to comptent_tech and dthorpe for their help. I created my DTO like so:

[XmlType("Object", Namespace = "", TypeName = "Object")]
public class dtoObject : CommonBase   
{       
    [XmlArray("SomeItems"), XmlArrayItem("SomeItem")]       
    public List<dtoSomeItem> SomeItems       
    {
        get { return _SomeItems; }           
        set { _SomeItems = value; }       
    }    
}

My function that does the serializing is this:

public string ToXML(List<dtoObject> oObject)
{
    XmlDocument xmlDoc = new XmlDocument();
    XmlSerializer xmlSerializer = new XmlSerializer(oObject.GetType(), new XmlRootAttribute("Objects"));
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("", "");

    using (MemoryStream xmlStream = new MemoryStream())
    {

        xmlSerializer.Serialize(xmlStream, oObject, ns);

        xmlStream.Position = 0;
        xmlDoc.Load(xmlStream);
        return xmlDoc.InnerXml;
    }
}

Doing this creates the xml format of:

<?xml version="1.0"?>
    <Objects>
        <Object>
            <SomeItems>
                <SomeItem>
                </SomeItem>
            </SomeItems>
        </Object>
    <Objects>    

Comments

0

You'll need something like XmlRootAttribute("Object", Namespace="", IsNullable=false) in front of the class declaration. That tells the XML serializer to use the <Object> tag to represent the dtoObject in the XML text.

As for the <Objects> tag, you won't get that unless you are serializing an array of dtoObject. That is beyond the scope of the dtoObject type.

3 Comments

dthorpe, thank you for the reply but when I add the Attribute like you suggested to my dtoObject class, the xml gets written to a string like this: <?xml version="1.0"?><ArrayOfDtoObject xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema"><dtoObject>...
How are you serializing the object to xml? Are you using System.Xml.Serialization.XmlSerializer.Save()?
I'm passing a List<dtoObject> to this function public string ToXML(Object oObject) { XmlDocument xmlDoc = new XmlDocument(); XmlSerializer xmlSerializer = new XmlSerializer(oObject.GetType()); using (MemoryStream xmlStream = new MemoryStream()) { xmlSerializer.Serialize(xmlStream, oObject); xmlStream.Position = 0; xmlDoc.Load(xmlStream); return xmlDoc.InnerXml; } }

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.