Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > Serialization with XmlSerializer: how to set the XML root node to something different from <ArrayOfClassname>????

Reply
Thread Tools

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

 
 
Bob Rock
Guest
Posts: n/a
 
      06-16-2004
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


 
Reply With Quote
 
 
 
 
Mickey Williams
Guest
Posts: n/a
 
      06-16-2004
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


 
Reply With Quote
 
 
 
 
Bob Rock
Guest
Posts: n/a
 
      06-16-2004
> 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






 
Reply With Quote
 
Ming Chen [.NET MVP]
Guest
Posts: n/a
 
      06-17-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
how to create a node set of elements through C++ code while executing a style sheet and process the created node set. pvssvikas@gmail.com XML 0 01-25-2006 12:48 PM
avoiding XML serialization, different WSDL generation, soap serialization Ramunas Urbonas ASP .Net Web Services 1 07-27-2004 09:57 PM



Advertisments