Re: Serialization with XmlSerializer: how to set the XML root node to something different from <ArrayOfClassname>????

From: Dino Chiesa [Microsoft] (dinoch_at_online.microsoft.com)
Date: 06/16/04


Date: Wed, 16 Jun 2004 17:28:53 -0400


> 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???

Methods can return instances of Foo. Those instances of Foo will behave as
indicated by the attributes on Foo.
If your method returns an array, then the attributes that apply are those on
the base type.
So if it is an array of Foo that is being returned, then the attributes on
the Foo definition will apply to the elements of the array.

I believe it is not possible to effectively attribute arrays if they are not
serialized as members of classes. In other words, if it is a local variable
and you want to serialize it, I believe the attributes do not apply. Also
if the array is a member variable and you do not serialize the entire class,
then I believe the attributes do not apply.

> 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.

Yes this should work, but I don't understand what the problem is on
de-serializing. If you are using the same kind of XmlSerializer
(constructed with the XmlRootAttribute), then the string or stream with the
modified root node should de-serialize just fine. eg

        XmlRootAttribute xRoot = new XmlRootAttribute();
        xRoot.Namespace = "urn:www.example.org";
        xRoot.ElementName = "Bunch";

        s= new XmlSerializer(typeof(Fred[]), xRoot);
        Fred[] f= {.....};

        // serialize to a string:
        StringWriter sw = new StringWriter();
        s.Serialize( sw, f );
        string serializedXml = sw.ToString(); // will give you a modified
root element name

        // now de-serialize
        StringReader sr= new StringReader(serializedXml);
        Fred[] f2= (Fred[]) s.Deserialize(new System.Xml.XmlTextReader(sr));

>Sorry for the cross-posting, but it is done only in the hope that more
people may read (and hopefully answer) my post.

Uh huh. I hate to be a stick in the mud about the cross posting, but 1st, I
can't help it, it's my nature; and 2nd, this seems like a really really
basic case. You are asking specifically about xml serialization, and there
is a specific group for xml serialization. A perfect match! Posting more
widely doesn't make sense to me. Yes, more people see it. But not more
people who care, or who will have anything to contribute. You could also
ask xml serialization questions to people you meet in the subway on the way
to work, or at church, or in the stands at the ballgame, but I doubt you'll
have much success there either.

Imagine if everyone followed your policy. Every question on every topic
would get asked on every group. Would that work? The different groups are
set up to raise the signal-to-noise ratio. If only works if the
participants follow the conventions.

Sometimes cross-posts make sense because it is not clear what area the
question would fall under. 2 groups is reasonable. 3 is about the limit.
4 is egregious.

I am not making any of this up. This is just standard netiquette.
http://www.google.com/search?hl=en&num=30&q=netiquette+cross+posting

-D

"Bob Rock" <nospam.yet_another_apprentice@hotmail.com> wrote in message
news:%23jNQDo8UEHA.3420@TK2MSFTNGP12.phx.gbl...
> > 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
> >
>



Relevant Pages