Serialization with XmlSerializer: how to set the XML root node to something different from <ArrayOf

Discussion in 'ASP .Net Web Services' started by Bob Rock, Jun 16, 2004.

  1. Bob Rock

    Bob Rock Guest

    Hello,

    when serializing an array of elements of a class Classname using
    XmlSerializer.Serialize() I get an XML like the following:

    <?xml version="1.0">
    <ArrayOfClassname>
    .......
    .......
    </ArrayOfClassname>

    I'd like to be able to set the XML root node to something different from
    <ArrayOfClassname> .... for example something like <Classnames>.
    As an alternative when deserilizing an XML such as the following:

    <?xml version="1.0">
    <Classnames>
    .......
    .......
    </Classnames>

    I'd like to be able to "load" it into an array of objects of class Classname
    (at the moment when making such an attempt I get an obvious exception
    stating "<Classnames> was not expected").


    Bob Rock
     
    Bob Rock, Jun 16, 2004
    #1
    1. Advertising

  2. Maybe not exactly what you want, but you can get close like this:

    public class Foo
    {
    ArrayList _classNames = new ArrayList();
    [XmlArray("ClassNames"), XmlArrayItem("ClassName")]
    public string[] ClassNames
    {
    get { return (string[])_classNames.ToArray(typeof(string)); }
    set {_classNames = new ArrayList(value);}
    }
    }

    Construct and serialize a Foo like this:

    string[] names = new string[]{"f", "g", "h"};
    Foo f = new Foo();
    f.ClassNames = names;
    Console.WriteLine(SerializeThingToXmlString(f));


    Where the helper method is:

    static string SerializeThingToXmlString(object thing)
    {
    StringWriter stringWriter = new StringWriter();
    XmlSerializer serializer = new XmlSerializer(thing.GetType());
    serializer.Serialize(stringWriter, thing);
    return stringWriter.ToString();
    }

    --
    Mickey Williams
    Author, "Microsoft Visual C# .NET Core Reference", MS Press
    www.servergeek.com/blogs/mickey
     
    Mickey Williams, Jun 16, 2004
    #2
    1. Advertising

  3. Bob Rock

    Bob Rock Guest

    > Maybe not exactly what you want, but you can get close like this:
    >
    > public class Foo
    > {
    > ArrayList _classNames = new ArrayList();
    > [XmlArray("ClassNames"), XmlArrayItem("ClassName")]
    > public string[] ClassNames
    > {
    > get { return (string[])_classNames.ToArray(typeof(string)); }
    > set {_classNames = new ArrayList(value);}
    > }
    > }
    >
    > Construct and serialize a Foo like this:
    >
    > string[] names = new string[]{"f", "g", "h"};
    > Foo f = new Foo();
    > f.ClassNames = names;
    > Console.WriteLine(SerializeThingToXmlString(f));
    >
    >
    > Where the helper method is:
    >
    > static string SerializeThingToXmlString(object thing)
    > {
    > StringWriter stringWriter = new StringWriter();
    > XmlSerializer serializer = new XmlSerializer(thing.GetType());
    > serializer.Serialize(stringWriter, thing);
    > return stringWriter.ToString();
    > }
    >
    > --
    > Mickey Williams
    > Author, "Microsoft Visual C# .NET Core Reference", MS Press
    > www.servergeek.com/blogs/mickey
    >


    Ahhh, so that is the way you can use the XmlArrayAttribute and
    XmlArrayItemAttribute on something that is not a field!!!
    Is it possible to use it on methods returning arrays???

    I've also seen that there is a way of setting the root node element to
    whatever one wants using the XmlSerializer(Type, XmlRootAttribute)
    constructor. Unfortunately when deserializing I need to change the root node
    element to <ArrayOfClassname> as the XmlSerializer expects to avoid the
    exception.


    Bob Rock
     
    Bob Rock, Jun 16, 2004
    #3
  4. Hi, Bob.
    One solution I can think right out-of-the box is:

    public class Classnames {
    [XmlElement("Classname")]
    public Classname[] Members;
    ......
    }

    By applying XmlElementAttribute to an array, You can eliminate the
    ArrayOfClassname/Members element from the serialized result. And by wrapping
    the array in a class named Classnames, you can make sure the result Xml has
    a Classnames root element. The net effect is that you get an Xml document
    that has a Classnames root and a list of Classname.

    On the other hand, .NET does allow XmlXXXAttributes to be applied on
    function return value. But there isn't any easy way to leverage it. E.g:
    WebService engine utilizes this through XmlMapping, which is marked "not
    intended to be used directly from your code" in MSDN. I think that you can
    still use XmlMapping classes, though.

    Hope this helps.
    Ming Chen [.NET MVP]

    "Bob Rock" <> wrote in message
    news:...
    > Hello,
    >
    > when serializing an array of elements of a class Classname using
    > XmlSerializer.Serialize() I get an XML like the following:
    >
    > <?xml version="1.0">
    > <ArrayOfClassname>
    > ......
    > ......
    > </ArrayOfClassname>
    >
    > I'd like to be able to set the XML root node to something different from
    > <ArrayOfClassname> .... for example something like <Classnames>.
    > As an alternative when deserilizing an XML such as the following:
    >
    > <?xml version="1.0">
    > <Classnames>
    > ......
    > ......
    > </Classnames>
    >
    > I'd like to be able to "load" it into an array of objects of class

    Classname
    > (at the moment when making such an attempt I get an obvious exception
    > stating "<Classnames> was not expected").
    >
    >
    > Bob Rock
    >
    >
     
    Ming Chen [.NET MVP], Jun 17, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,648
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,505
    Dimitre Novatchev
    Aug 24, 2006
  3. kpg
    Replies:
    0
    Views:
    686
  4. Ramunas Urbonas
    Replies:
    1
    Views:
    426
    Dino Chiesa [Microsoft]
    Jul 27, 2004
  5. cmay

    Is Serialization done with Soap or XmlSerializer

    cmay, Jun 8, 2006, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    167
    Josh Twist
    Jun 11, 2006
Loading...

Share This Page