Serialized object to XmlDocument

  • Thread starter Thread starter Pierre
  • Start date Start date
P

Pierre

Hi,

I would like to apply a Xsl Transformation on a serialized object. But
I don't know where to start.

Should I do a XmlDocument.load(ObjectToSerialize.ToString())?

Or should I call the ObjectToSerialize.Serialize() before? In wich case
I dun understand why I can't access the .Serialize() on my Class:

[Serializable]
public class MyBusinessList{
[XmlElement("BusinessLists")]
public BusinessList[] _busList;

public MyBusinessList(){
this._busList=new BusinessList[1];
}

public void addBusiness(BusinessList tempBus){
this._busList=this.resizeArray(this._busList,
this._busList.Length+1);
this._busList[this._busList.Length-1]=tempBus;
}

public BusinessList[] resizeArray (BusinessList[] oldArray, int
newSize) {
BusinessList[] tempArr=new BusinessList[newSize];
oldArray.CopyTo(tempArr, 0);
return tempArr;
}
}

Thx in advance for your suggestions and answers,
Pierre
 
SerializableAttribute is a marker that let's the remoting / serialization framework know that your class can be passed by-value. It
does not create a "Serialize()" method and does not change the output of your classes "ToString()" method in any way.

In order to serialize a class marked as serializable, all class members must also be serializable (i.e. the object named,
"BusinessList" in your example must be serializable by attribute or implementation).

You can also have custom serialization by implementing the ISerializable interface and providing a serialization constructor.
(http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconCustomSerialization.asp)

In order to actually serialize your class, you need a "formatter". There are two basic formatters provided by the framework:
BinaryFormatter and SoapFormatter. These classes are utilities that can transform a serializable object to and from an array of
Bytes or XML, respectively.

Formatters in the MSDN Developer's Guide:
(XML and SOAP: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconserialization.asp)
(BINARY: http://msdn.microsoft.com/library/d.../html/cpconbinaryserialization.asp?frame=true)

Formatting may sound complex, but it's actually quite simple. You really don't have to do much work in most cases. Just mark your
class as [Serializable], ensure that all members to-be serialized are also, serializable, and pass the object to an instance of the
appropiate formatter.

In all situations that I can see, BinaryFormatter will perform better and reduce network traffic since the object graph size will be
much smaller in comparison to XML-wrapped SOAP, yet the standardized SOAP protocol may be a requirement for your system.

GL

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
Pierre said:
Hi,

I would like to apply a Xsl Transformation on a serialized object. But
I don't know where to start.

Should I do a XmlDocument.load(ObjectToSerialize.ToString())?

Or should I call the ObjectToSerialize.Serialize() before? In wich case
I dun understand why I can't access the .Serialize() on my Class:

[Serializable]
public class MyBusinessList{
[XmlElement("BusinessLists")]
public BusinessList[] _busList;

public MyBusinessList(){
this._busList=new BusinessList[1];
}

public void addBusiness(BusinessList tempBus){
this._busList=this.resizeArray(this._busList,
this._busList.Length+1);
this._busList[this._busList.Length-1]=tempBus;
}

public BusinessList[] resizeArray (BusinessList[] oldArray, int
newSize) {
BusinessList[] tempArr=new BusinessList[newSize];
oldArray.CopyTo(tempArr, 0);
return tempArr;
}
}

Thx in advance for your suggestions and answers,
Pierre
 
Hi and thx for the answer.

My problem is not to serialize the object then, but to load the
serialized object into a XmlDocument without writing a file on the FS.

let's say I have an object serializable: BusinessEntity, a xsl matching
it's nodes and a class capable of doing a Xslt with a XmlDocument and a
Xsl.

I have tryed that kind of things:

XmlSerializer ser = new XmlSerializer(typeof(BusinessEntity));
XmlDocument xmlDoc = new XmlDocument();
BusinessEntity busEnt = new BusinessEntity();
....
ser.Serialize(xmlDoc, busList);

myXsltprocess.Xslt(xmlDoc, Xsl);

But that doesn't work as I'de need a XmlWriter or a IO
Stream/TexWritter.

Is there a simple solution to do that (I mean without writting a file
on the fs)?

Thx in advance for your help/advices.
 
You can serialize to a System.IO.MemoryStream instance.

XmlSerializer ser = new XmlSerializer(typeof(BusinessEntity));
BusinessEntity busEnt = new BusinessEntity();

using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
ser.Serialize(stream, busEnt);

// You'll have to modify your class to accept a Stream argument:
myXsltprocess.Xslt(stream, Xsl);
}


Just curious, but why do you need to perform an xsl transformation on a serialized object graph? It seems like there may be a
better approach to the problem at hand.
 
Hi,

Thx for the answer I'm going to try that.

That's the easiest solution I found to display complex data without
having to do many loops and to code the whole display part.

My idea is to just fill the object and then display it (with all the
other objects it includes).

Dunno if yu're familiar with the UDDI types but to summerize I want to
retrieve the higher level object in a XmlDocument and after manipulate
only that on the user interface.

If you have any suggestions I have my hears wide opened :)
 
It's an interesting approach, but I think it parallels data binding. Making a true GUI isn't very difficult, especially when you
use data binding.

If your trying to make your objects editable over the web, or in a WinForms app, you can use data-binding techniques that will do
most, if not all, of the work for you. Also, you'll have much more of a rich client for editing, validation, etc. then you would in
an Xml file.

How are you going to edit the xml, and deserialize it? In other words, aren't you going to be coding an interface for this anyway?
 
I forgot to mention one more thing that may be of interest:

System.Windows.Forms.PropertyGrid

The VS.NET property grid is a control that you can use in WinForms apps too. Just set SelectedObject to an instance of your object
and without serialization it will allow you to edit the public properties of the object. Also, using metadata in your code
(Attributes) you can specify certain configurable settings for the property grid that will be hosting your object.

using System;
using System.ComponentModel;

[Category("MyCustomCategory"), DefaultValue(true), Description("A boolean property!")]
public bool BoolProp
{
get { return bp; } set { bp = value; }
}


If it's a web app your after, then this will not help you.
 
Back
Top