Can't seem to drop "AnyType" element when serializing...

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi there,
Ive got a windows form that uses data binding. Data binding is using the BindingList, with datasource set to a custom business object. Im pretty close -- but even after creating a fully loaded XmlSerializer using the constructor with 5 parameters,
it still doesnt seem to recognize the attribute overrides parameter.
Heres an example class :
<div style="color:black; background-color:white
<pre><span style="color:blue Public <span style="color:blue Class MyObject
<span style="color:blue Sub <span style="color:blue New( s <span style="color:blue as <span style="color:blue String )
Test = s
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue Private _test <span style="color:blue as <span style="color:blue string
<span style="color:blue Public <span style="color:blue Property Test() <span style="color:blue As <span style="color:blue String
<span style="color:blue Get
<span style="color:blue Return _test
<span style="color:blue End <span style="color:blue Get
<span style="color:blue Set(<span style="color:blue ByVal value <span style="color:blue As <span style="color:blue String)
_test = value
<span style="color:blue End <span style="color:blue Set
<span style="color:blue End <span style="color:blue Property
<span style="color:blue End <span style="color:blue Class
[/code]



Serialization code:

<div style="color:black; background-color:white
<pre>oList = <span style="color:blue New System.Windows.Forms.BindingSource()
oList.DataSource = <span style="color:blue GetType(MyObject)
oList.add( <span style="color:blue New MyObject(<span style="color:#a31515 "test value 1") )
oList.add( <span style="color:blue New MyObject(<span style="color:#a31515 "test value 2") )

<span style="color:blue Dim mytype <span style="color:blue As Type = <span style="color:blue GetType(MyObject)
<span style="color:blue Dim typelist() <span style="color:blue As Type = {mytype}
<span style="color:blue Dim root <span style="color:blue As <span style="color:blue New Xml.Serialization.XmlRootAttribute(<span style="color:#a31515 "RootElement")
<span style="color:blue Dim myAttr <span style="color:blue As <span style="color:blue New Xml.Serialization.XmlElementAttribute(<span style="color:#a31515 "Item", mytype )
<span style="color:blue Dim myAttrs <span style="color:blue As <span style="color:blue New Xml.Serialization.XmlAttributes()
myAttrs.add( myAttr )
<span style="color:blue Dim attrOverrides <span style="color:blue As <span style="color:blue New Xml.Serialization.XmlAttributeOverrides()
attrOverrides.Add(oList.<span style="color:blue GetType, <span style="color:#a31515 "List", attrs) <span style="color:green again, oList is BindingSource
<span style="color:blue Dim serializer <span style="color:blue as Xml.Serialization.XmlSerializer(oList.<span style="color:blue GetType(), attrOverrides, typelist, root, <span style="color:#a31515 "MyNS")

<span style="color:blue Dim xs <span style="color:blue As <span style="color:blue New Xml.XmlWriterSettings
xs.OmitXmlDeclaration = <span style="color:blue True

<span style="color:blue Dim sw <span style="color:blue As <span style="color:blue New IO.StringWriter()
<span style="color:blue Dim xw <span style="color:blue As Xml.XmlWriter = Xml.XmlTextWriter.Create(sw, xs)
xm.Serialize(xw, oList)
sb.Append(sw.ToString)
msgbox (sb.toString)
[/code]



And XML result:
<div style="color:black; background-color:white
<pre><span style="color:blue <<span style="color:#a31515 RootElement <span style="color:red xmlns:xsi<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.org/2001/XMLSchema-instance<span style="color:black " <span style="color:red xmlns:xsd<span style="color:blue =<span style="color:black "<span style="color:blue http://www.w3.irg/2001/XMLSchema<span style="color:black " <span style="color:red xmlns<span style="color:blue =<span style="color:black "<span style="color:blue MyNS<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 anyType <span style="color:red xsi:type<span style="color:blue =<span style="color:black "<span style="color:blue MyObject<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Test<span style="color:blue >Test value 1<span style="color:blue </<span style="color:#a31515 Test<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 anyType<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 anyType <span style="color:red xsi:type<span style="color:blue =<span style="color:black "<span style="color:blue MyObject<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Test<span style="color:blue >Test value 2<span style="color:blue </<span style="color:#a31515 Test<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 anyType<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 RootElement<span style="color:blue >
[/code]


The question I have is how do I change <anyType xsl:type="MyClass to <Item> ?
* note much of the code has been left out for brevitys sake. The serialization code is intended to run once the form has closed, and its assumed that the user will make numerous changes to it while open, and the BindingSource will have all the data
once finished *
<br/>
<br/>

View the full article
 
Back
Top