Generating XML from an object

  • Thread starter Thread starter ludwig_stuyck
  • Start date Start date
L

ludwig_stuyck

Hi,

I have an object of a specific type (this type has properties which
can be complex objects etc...) and I want to serialize this object to
XML so that every property and every child object is serialized, also
the ones that are null.

Does anyone have an idea on how to do that?

Thanks!
 
I have an object of a specific type (this type has properties which
can be complex objects etc...) and I want to serialize this object to
XML so that every property and every child object is serialized, also
the ones that are null.

Does anyone have an idea on how to do that?

There is XmlSerializer
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
and with .NET 3.0 and later there is also DataContractSerializer
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx
 
There is XmlSerializerhttp://msdn.microsoft.com/en-us/library/system.xml.serialization.xmls...
and with .NET 3.0 and later there is also DataContractSerializerhttp://msdn.microsoft.com/en-us/library/system.runtime.serialization....

Properties that are null are not serialized this way, and I need these
too in de XML (with default values).
 
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry

Are you talking about serializing without using Xml type-ish attributes?

My entry is very 1.1'ish, fyi.

But I have objects, subcollections, etc, etc.  (Aka, not just one level
deep).









- Tekst uit oorspronkelijk bericht weergeven -

Yes, just a plain object that has child objects (object hierarchy) of
some of which can be null. But I need everything in the XML, also the
null-values.
 
Properties that are null are not serialized this way, and I need these
too in de XML (with default values).

Then why not simply make the properties not be null i.e. give them a
default entity?

That way you won't be fighting the system... kinda by definition a
null doesn't *have* sub-values, so trying to write null objects (and
their sub-properties) is counter-intuitive.

Marc
 
Could you perhaps give a really simple example (a *short* example) of
what you mean? i.e. "I want this object tree [...] to serialize into
this xml [...]"

Marc
 
Could you perhaps give a really simple example (a *short* example) of
what you mean? i.e. "I want this object tree [...] to serialize into
this xml [...]"

Marc

A little clarification: I have written a client app that dynamically
retrieves the WSDL for a running service, creates a proxy for it and
now I'm trying to call the service operations dynamically using the
proxy. This works.

However, the idea is that you can edit the service operation
parameters before invoking the operations. Therefore I need a way to
visualize these parameters, which are plain .NET objects.

To do that I was thinking I could serialize these objects to XML and
display the XML, then you can change the values in the XML and
afterwards the XML is deserialized back into the object and used as a
parameter to invoke the service operation. But serializing an object
results in an XML where only the non-null properties are generated,
and I need all properties to be generated.

Another solution was to build a hierarchy of the object in a treeview
and make every property editable, but this involves a lot of work.

So I'm still not sure what could be the best way to go here...
 
Back
Top