-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: XmlSerialization?
PostPosted: Thu Jun 16, 2005 5:51 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
I was reading the old forums and I'm wondering if I've just hit a wall. In certain use cases I need to serialize my object graph to an XML string for use by a specific UI layer. Do I have any options? Specifically:

Code:
[Serializable]
public class Foo {
  private int id;
  private string name;
  private IList bars;

  //the usual properties ...

  public getXML() {
    // code that creates memory stream, XmlTextWriter,
    // XmlSerializer and returns utf string
  }
}

[Serializable]
public class Bar {
  private int id;
  private string name;
  private Foo foo;

  //the usual properties ...
}


Mapping file:

Code:
<class name="Foo">
  <id name="id" access="field">
    <generator class="native" />
  </id>

  <property name="Name" />

  <bag name="Bars">
    <key column="BarID" />
    <one-to-many class="Bar" />
  </bag>
</class>

<class name="Bar">
  <id name="id" access="field">
    <generator class="native" />
  </id>

  <property name="Name" />

  <many-to-one name="Foo" class="Foo" />
</class>


when i try to serialize this. the following exception is thrown:

Quote:
The type AssemblyName.Bar was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.


any pointers would be welcomed with delight.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 16, 2005 9:24 pm 
Regular
Regular

Joined: Mon May 16, 2005 1:35 am
Posts: 67
If you are serializing as a result of passing or returning an instance of Foo to or from a WebMethod on a WebService then you will need to specify the [XmlInclude(typeof(Bar))] attribute on the WebMethod (alongside the WebMethod() attribute.

This is a result of the fact that the WebService base class did not find the Bar class when inspecting the WebMethod in question via reflection. It needs to be told explicitly to expect it via the XmlInclude attribute.

If you are using the XmlSerializer class directly, then you will need to apply the [XmlInclude(typeof(Bar)] attribute to the Foo class directly. This should give the XmlSerializer the information it needs to correctly serialize/deserialize Foo objects.

Also note that for a class to be XmlSerializable, it requires a public constructor. Also, only properties with both a public getter and setter will end up being serialized.

Furthermore, you do not require the [Serializable] attribute to be applied to a class for it to be serializable using the XmlSerializer. The [Serializable] attribute simply informs the .NET runtime that the class should be marshalled by value when using .NET remoting. For a class to be marshalled by .NET remoting, it must have either the [Serializable] attribute or inherit from MarshalByRefObject.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 17, 2005 6:16 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
bags,

thanks so much for the pointers. i've been able to get serialization working with your help. i'm not completely satisfied with the default XML document so I think I'll have to learn more about configuring the parser, or i'll just have to write routines that build the xml the way I want it (with a healthy mix of elements and attributes as opposed to everything being an attribute).

Interestingly enough, collections are serialized even if there is no setter. This makes sense when I think about it but wasn't what I initially expected.

Cheers,

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 19, 2005 3:12 am 
Regular
Regular

Joined: Mon May 16, 2005 1:35 am
Posts: 67
Devon,

The collections are probably being serialized without a setter as they probably implement IXmlSerializable. The XmlSerializer will deserialize an object via the ReadXml() method on the IXmlSerializable interface rather than using the setter if the property supports IXmlSerializable.

With regards to the structure of the XML which is generated by the XmlSerializer object, this can be highly customised through use of attributes such as XmlRootAttribute, XmlElementAttribute and XmlAttributeAttribute. If you require even more customisation, then you can implement the IXmlSerializable interface yourself to get complete control.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.