Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : XmlSerialization to XmlNode


dmessenger NO[at]SPAM verdantsys.com
2/13/2005 2:34:49 PM
I have a schema that I load into classes that mirror the schema using
the XmlSerialization. The schema contains an xsd:Any that I load into
'Extra' as follows:

[XmlElement(ElementName="Extra")] public XmlNode Extra { get { return
extra;} set { extra = value;}}

However, if I have:

<Something>
<Extra>
<a>something</a>
<b>another thing</b>
</Extra>
</Something>

I would expect Extra to contain an XmlNode of :

<Extra>
<a>something</a>
<b>another thing</b>
</Extra>

But it doesn't : it contains :

<a>something</a>

Is there any attributes etc I can set to get the full element to be
retained ?

TIA

Dino Chiesa [Microsoft]
2/14/2005 5:57:02 PM
Yes, when I use

// [XmlElement(ElementName="Extra")]
[XmlAnyElement]
public System.Xml.XmlElement Any;

I get the behavior you seek.
note: the attribute is XmlAnyElement, not XmlElement. Also, omit the name.


If you want to allow more than one element, like so:

<Something>
<Extra>
<a>something</a>
<b>another thing</b>
</Extra>
<foo/>
<bar>
<blah/>
</bar>
</Something>

then specify an array:


[XmlAnyElement]
public System.Xml.XmlElement[] Any;



-Dino


[quoted text, click to view]

AddThis Social Bookmark Button