473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

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.Customer
{
[XmlRoot(ElementName = "application", IsNullable = false, Namespace =
"http://meritconsulting.no/MovexApplicationProperties.xsd")]
[XmlSchemaProvider("GetSchemaFile")]
public class MovexApplicationProperties : IXmlSerializable
{
public const string ApplicationType = "movex";
public const int Version = 2;
private string _administratorUser;
private string _administratorPassword;
private string _servicePack;
private string _movexVersion;
private
List<MeritAdmin.Customer.MovexApplicationPropertie s.Environment>
_environments = new
List<MeritAdmin.Customer.MovexApplicationPropertie s.Environment>();

#region XmlSchema

public static XmlSchemaComplexType GetSchemaFile(XmlSchemaSet
schemaSet)
{
string xsdFile = Directory.GetCurrentDirectory() +
"\\Customer\\MovexApplicationProperties.xsd";
XmlSerializer schemaSerializer = new
XmlSerializer(typeof(XmlSchema));
XmlSchema schema =
(XmlSchema)schemaSerializer.Deserialize(XmlReader. Create(xsdFile));
schemaSet.Add(schema);

// Target namespace
string tns =
"http://meritconsulting.no/MovzexApplicationProperties.xsd";
XmlQualifiedName application = new
XmlQualifiedName("application", tns);
XmlSchemaComplexType applicationType =
(XmlSchemaComplexType)schema.SchemaTypes[application];
return applicationType;
}

#endregion

#region IXmlSerializable Members

public System.Xml.Schema.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/MovexApplicationProperties.xsd";
writer.WriteAttributeString("type",
MovexApplicationProperties.ApplicationType);
writer.WriteElementString("version",
MovexApplicationProperties.Version.ToString());
writer.WriteElementString("administratorUser",
this.AdministratorUser);
writer.WriteElementString("administratorPassword",
this.AdministratorPassword);
writer.WriteElementString("servicePack", this.ServicePack);
writer.WriteElementString("movexVersion", this.MovexVersion);
writer.WriteElementString("environments", ns, string.Empty);

// This is not the right way to do it
//foreach (Environment environment in this.Environments)
//{
// environment.WriteXml(writer);
//}
}

#endregion
[XmlRoot(ElementName = "environment", IsNullable = true, Namespace =
"http://meritconsulting.no/MovexApplicationProperties.xsd")]
[XmlSchemaProvider("GetSchemaFile")]
public class Environment : IXmlSerializable
{
private string _name;
private string _portNumber;

#region XmlSchema

public static XmlSchemaComplexType GetSchemaFile(XmlSchemaSet
schemaSet)
{
string xsdFile = Directory.GetCurrentDirectory() +
"\\Customer\\MovexApplicationProperties.xsd";
XmlSerializer schemaSerializer = new
XmlSerializer(typeof(XmlSchema));
XmlSchema schema =
(XmlSchema)schemaSerializer.Deserialize(XmlReader. Create(xsdFile));
schemaSet.Add(schema);

// Target namespace
string tns =
"http://meritconsulting.no/MovexApplicationProperties.xsd";
XmlQualifiedName environment = new
XmlQualifiedName("environment", tns);
XmlSchemaComplexType environmentType =
(XmlSchemaComplexType)schema.SchemaTypes[environment];
return environmentType;
}

#endregion

#region IXmlSerializable Members

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

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

public void WriteXml(XmlWriter writer)
{
string ns =
"http://meritconsulting.no/MovexApplicationProperties.xsd";
writer.WriteElementString("name", ns, this.Name);
writer.WriteElementString("portNumber", ns,
this.PortNumber);
}

#endregion
}
}
}

And this is how I'm trying to serialize some test objects:
private void Form1_Load(object sender, EventArgs e)
{
MovexApplicationProperties movex = new
MovexApplicationProperties("test", "test", "12", "v12Java");
movex.Environments.Add(new
MovexApplicationProperties.Environment("TST", "16800"));
List<TypeextraTypes = new List<Type>();
extraTypes.Add(new
MovexApplicationProperties.Environment().GetType() );
XmlSerializer serializer = new XmlSerializer(movex.GetType(),
extraTypes.ToArray());
StringWriter stringWriter = new StringWriter();
serializer.Serialize(stringWriter, movex);
this.textBox1.Text = stringWriter.ToString();
}

Here is the result:
<?xml version="1.0" encoding="utf-16"?>
<application type="movex"
xmlns="http://meritconsulting.no/MovexApplicationProperties.xsd">
<version>2</version>
<administratorUser>test</administratorUser>
<administratorPassword>test</administratorPassword>
<servicePack>12</servicePack>
<movexVersion>v12Java</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 3379

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

Similar topics

2
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...
9
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...
5
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...
7
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...
1
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...
3
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
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...
0
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 ...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.