XML Serialization of Object with other object inside .net
I'm trying to XML-serialize a class the properties of which I use to format a document.
Basically, it is a class for the document's header and another class for its rows.Class diagram:
In this class diagram, the class I want to serialize is ExcelPrintCorte
that inherits its methods from ExcelCabec
and has a private member ExcelPrintDocumento
(and a public method to get it).
My purpose is to XML-serialize ExcelPrint开发者_开发技巧Corte
and save the inherited properties' values and also the properties' values of ExcelPrintDocumento
. I followed many guides to XML-serialize an object but it saves nothing but:
<?xml version="1.0" encoding="utf-8"?>
<ExcelPrintCorte xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
So, what am I doing wrong? Or is what I'm trying to do not possible with XML serialization?
Impossible to tell without code, but:
- the properties (or less commonly, fields) you want to serialize must be public
- the properties (blah) must be mutable, i.e. not
get
-only orreadonly
- anything marked
[XmlIgnore]
is ignored, and a few other rules like[DefaultValue]
,ShouldSerialize{foo}
etc are observed
Those are the rules that would stop anything showing; other errors that would cause exceptions (check the inner-exceptions etc):
- not public (including any containing types)
- no public parameterless constructor (including: must be concrete)
- unexpected sub-types (i.e. not previously advertised with
[XmlInclude]
) - some member-types;
object
, dictionaries, lists without an obviousAdd
, etc
精彩评论