Groups | Blog | Home
all groups > dotnet xml > november 2006 >

dotnet xml : Serialization of jagged Arrays Issue


Sahar
11/23/2006 10:57:09 PM
Hi there,

I m trying to return an object (of my own written class) from a web
service that contains jagged Arrays as public variables. Asp.Net is
showing me the its serialized version on the browser when i invoke the
service during test.

Code:
public class returnType
{
[System.Xml.Serialization.XmlElementAttribute("chassisdata")]
public _chassisdata[] chassisdata;

[XmlArray(),XmlArrayItem("wheel", typeof(_wheel[]),IsNullable=false)]
public _wheel[][] upsteps;


[XmlArray(),XmlArrayItem("wheelsize",
typeof(_wheelsize[]),IsNullable=false)]
public _wheelsize[][] alloywheel;

[System.Xml.Serialization.XmlAttributeAttribute()]
public string id;

}
Result:
<upsteps>
<wheels>
<_wheel>
....
....
</_wheel>
<_wheel>
....
....
</_wheel>
</wheel>
</upsteps>
<alloywheels>
<wheelsize>
<wheelsize size="">
....
....
</wheelsize>
<wheelsize size="">
....
....
</wheelsize>
</wheelsize>
</alloywheels>
--------------------------------------------------------
The Problem is,I want to change the serialized xml as follows

<upsteps>
<_wheel>
....
....
</_wheel>
<_wheel>
....
....
</_wheel>
</upsteps>
<alloywheels>
<wheelsize size="">
....
....
</wheelsize>
<wheelsize size="">
....
....
</wheelsize>
</alloywheels>

Can any body help me?

thanx in Advance.
Sahar
11/25/2006 8:36:23 AM
thanx a lot for replying Gadget, but the problem is i dont know how to
make asp.net to generate wsdl of my choice. OR how can i hand code a
schema from a set of classes.

secondly, upsteps is not a class, its a jagged array of the "wheel"
class which is written below. I want to know that what should i do to
generate a proper xml of "upsteps" variable.

class returnType
{
wheel[][] upsteps;
}
class wheel
{
public string name;

public wheelmanufacturer[] _wheel manufacturer;
}

thanx for replying again. i need the solution urgently please if you
can answer or refer to some appropriate knowledge base........I will b
greatfull.



[quoted text, click to view]
Gadget
11/25/2006 11:15:51 AM
[quoted text, click to view]

You can't remove the parent node in this way using the built-in
serialization.
The built in system is not intended to provide ways of generating XML for
use in other people's schemas. The fact it sometime can is a bonus, but
it's actually there to serialize and deserialize object in the most
efficient manner it can.
The serializer internally uses reflection to read the object type you
provided and builds a set of .NET code in C# involving XmlTextReaders and
writers. That's it, no more, no less. You can even view the source code if
you get the command line arguments right (Google for more info on doing
this).
If you really want a set of classes to match an external schema then
hand-code it yourself, or look at XSD.exe (although I often prefer the
hand-coding :)).

Cheers,
Gadget
11/25/2006 11:19:53 AM
I should add that the reason you can't remove the header node is because if
your class was something like:

class upsteps
{
WheelCollection wheels1;
WheelCollection wheels2;
}

then the deserializer would never know when the wheels1 collection had
finished and wheels2 started. Hence it needs the collection's node to tell
it to start the next object.
Ambiguity will not be tolerated :)

Cheers,
Gadget
11/26/2006 2:28:31 PM
[quoted text, click to view]

Well there is no built-in method to handle multidimensional arrays, so you
either have to replace your existing solution with a collection of
collections, or write your own serializer/deserializer.
If the format of the XML is important then the second method is ideal. You
could simply have nested loops, starting an element tag in the outer,
looping through the variable-length inner, then closing the tag.
Look at XmlTextWriter; it's very easy to use.
You can then embed this in your class using the IXmlSerializable interface.
See
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx
for details.

Cheers,
AddThis Social Bookmark Button