473,785 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Complex XML Serialization

I've been reading Keith Pijanowski's article - Enrich Your XML Serialization
With Schema Providers In The .NET Framework - on how to do custom
serialization of objects. Link to the article:
http://msdn.microsoft.com/msdnmag/is...t/default.aspx

Now I'm trying to do the same ting in a more complex scenario, with two
nested classes. The nested class is used in a collection property of the
main class, and I want that collection to be automatically serialized. Here
is a copy of the source without properties and contructors:

namespace MeritAdmin.Cust omer
{
[XmlRoot(Element Name = "applicatio n", IsNullable = false, Namespace =
"http://meritconsulting .no/MovexApplicatio nProperties.xsd ")]
[XmlSchemaProvid er("GetSchemaFi le")]
public class MovexApplicatio nProperties : IXmlSerializabl e
{
public const string ApplicationType = "movex";
public const int Version = 2;
private string _administratorU ser;
private string _administratorP assword;
private string _servicePack;
private string _movexVersion;
private
List<MeritAdmin .Customer.Movex ApplicationProp erties.Environm ent>
_environments = new
List<MeritAdmin .Customer.Movex ApplicationProp erties.Environm ent>();

#region XmlSchema

public static XmlSchemaComple xType GetSchemaFile(X mlSchemaSet
schemaSet)
{
string xsdFile = Directory.GetCu rrentDirectory( ) +
"\\Customer\\Mo vexApplicationP roperties.xsd";
XmlSerializer schemaSerialize r = new
XmlSerializer(t ypeof(XmlSchema ));
XmlSchema schema =
(XmlSchema)sche maSerializer.De serialize(XmlRe ader.Create(xsd File));
schemaSet.Add(s chema);

// Target namespace
string tns =
"http://meritconsulting .no/MovzexApplicati onProperties.xs d";
XmlQualifiedNam e application = new
XmlQualifiedNam e("application" , tns);
XmlSchemaComple xType applicationType =
(XmlSchemaCompl exType)schema.S chemaTypes[application];
return applicationType ;
}

#endregion

#region IXmlSerializabl e Members

public System.Xml.Sche ma.XmlSchema GetSchema()
{
throw new Exception("The method or operation is not
implemented.");
}

public void ReadXml(System. Xml.XmlReader reader)
{
throw new Exception("The method or operation is not
implemented.");
}

public void WriteXml(System .Xml.XmlWriter writer)
{
string ns =
"http://meritconsulting .no/MovexApplicatio nProperties.xsd ";
writer.WriteAtt ributeString("t ype",
MovexApplicatio nProperties.App licationType);
writer.WriteEle mentString("ver sion",
MovexApplicatio nProperties.Ver sion.ToString() );
writer.WriteEle mentString("adm inistratorUser" ,
this.Administra torUser);
writer.WriteEle mentString("adm inistratorPassw ord",
this.Administra torPassword);
writer.WriteEle mentString("ser vicePack", this.ServicePac k);
writer.WriteEle mentString("mov exVersion", this.MovexVersi on);
writer.WriteEle mentString("env ironments", ns, string.Empty);

// This is not the right way to do it
//foreach (Environment environment in this.Environmen ts)
//{
// environment.Wri teXml(writer);
//}
}

#endregion
[XmlRoot(Element Name = "environmen t", IsNullable = true, Namespace =
"http://meritconsulting .no/MovexApplicatio nProperties.xsd ")]
[XmlSchemaProvid er("GetSchemaFi le")]
public class Environment : IXmlSerializabl e
{
private string _name;
private string _portNumber;

#region XmlSchema

public static XmlSchemaComple xType GetSchemaFile(X mlSchemaSet
schemaSet)
{
string xsdFile = Directory.GetCu rrentDirectory( ) +
"\\Customer\\Mo vexApplicationP roperties.xsd";
XmlSerializer schemaSerialize r = new
XmlSerializer(t ypeof(XmlSchema ));
XmlSchema schema =
(XmlSchema)sche maSerializer.De serialize(XmlRe ader.Create(xsd File));
schemaSet.Add(s chema);

// Target namespace
string tns =
"http://meritconsulting .no/MovexApplicatio nProperties.xsd ";
XmlQualifiedNam e environment = new
XmlQualifiedNam e("environment" , tns);
XmlSchemaComple xType environmentType =
(XmlSchemaCompl exType)schema.S chemaTypes[environment];
return environmentType ;
}

#endregion

#region IXmlSerializabl e Members

public XmlSchema GetSchema()
{
throw new Exception("The method or operation is not
implemented.");
}

public void ReadXml(XmlRead er reader)
{
throw new Exception("The method or operation is not
implemented.");
}

public void WriteXml(XmlWri ter writer)
{
string ns =
"http://meritconsulting .no/MovexApplicatio nProperties.xsd ";
writer.WriteEle mentString("nam e", ns, this.Name);
writer.WriteEle mentString("por tNumber", ns,
this.PortNumber );
}

#endregion
}
}
}

And this is how I'm trying to serialize some test objects:
private void Form1_Load(obje ct sender, EventArgs e)
{
MovexApplicatio nProperties movex = new
MovexApplicatio nProperties("te st", "test", "12", "v12Java");
movex.Environme nts.Add(new
MovexApplicatio nProperties.Env ironment("TST", "16800"));
List<TypeextraT ypes = new List<Type>();
extraTypes.Add( new
MovexApplicatio nProperties.Env ironment().GetT ype());
XmlSerializer serializer = new XmlSerializer(m ovex.GetType(),
extraTypes.ToAr ray());
StringWriter stringWriter = new StringWriter();
serializer.Seri alize(stringWri ter, movex);
this.textBox1.T ext = stringWriter.To String();
}

Here is the result:
<?xml version="1.0" encoding="utf-16"?>
<application type="movex"
xmlns="http://meritconsulting .no/MovexApplicatio nProperties.xsd ">
<version>2</version>
<administratorU ser>test</administratorUs er>
<administratorP assword>test</administratorPa ssword>
<servicePack>12 </servicePack>
<movexVersion>v 12Java</movexVersion>
<environments />
</application>

As you can see, the environments tag isn't aware of the contents of the
collection, and the environment doesn't get serialized even though I've
passed the Environment type as extra types to the XmlSerializer. What is the
right way to do this?

---
Eivind

Dec 10 '06 #1
0 3395

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5177
by: Phil Da Lick! | last post by:
Hello, Anyone got any good examples of how to serialize a nested class? My outer class has got ArrayList members for inner classes, each with their own data. I need to serialise the lot into an xml file for easy load/save.
9
1243
by: Robert W. | last post by:
I have built several kinds of complex classes that I work with in my program. Storing them to disk is no problem because I just pass the instantiated object to a SaveData method, accepting it as a generic "object". Then I use reflection to iterate through the object and save the data. But loading the data from disk seems to be anything but generic. I have several challenges with this but here's one of the first: public void OpenData...
5
3483
by: Trail Monster | last post by:
Ok, I've been searching the net now for several days and can't find how to do this anywhere. Version: VS 2005 Professional Release, 2.0 Framework Background: I have a complex business object Employee that contains public properties and several nested objects such as a Spouse object and a List of Coverage objects.
7
11930
by: BS | last post by:
Hello everybody I'm calling a webservice that returns complex data. The goal is to populate a datagrid with it. Using a loop for each record found ( such as For i = 0 To oResponse.historicalData.Length - 1 ) no problem to load a datagrid. However, when I try to bind directly the datasource to the web service
1
4986
by: GAURAV KRISHNA | last post by:
I am able to deserialize an array using XMLSerializer but the size of an array is 0.The problem seems to be because of unqualified element name but I am not very sure. Here is what I did: I get the following xml from the server ===================================================
3
15106
by: Paulo Morgado [MVP] | last post by:
Hi all ..NET Framework 1.1 I have created several types that are serailized to XML as strings. Someting like this: public struct MyInt32 : IXmlSerializable { int value;
1
2862
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive types and of more elaborate classes implementing IXmlSerializable. The resulting WSDL file for the webservice has two separate schemas in its <types> sections, and the client proxy (generated with wsdl.exe) is missing the definitions of the...
0
3579
by: shaily | last post by:
hi I have a java web service called "Registration service" and C# client which is a consumer of that service java web service running under Tomcat has following interface API exposed public RegistrationResponse registerDevice(RegistrationRequest regReq) throws RemoteException
1
1609
by: sebastian.mattar | last post by:
HI there! I am trying to consume a web service implemented in Perl (SOAP::Lite) using VS Express 2008. I already implemented a client in java without problems. I succeeded in calling the web service, but the SOAP-Call returns a complex type which is not handled correctly. Invoke() throws an exception stating that an object of type system.String can not be assigned to type RetDataType. Debugging shows
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.