Using Binary Serialization on XCData (or a Deep Copy Issue...)

kriswinner

Member
Joined
Apr 23, 2009
Messages
23
Programming Experience
10+
Some background:
I'm creating an application that uses several "multi-leveled" structures (structures within structures within structures....) to store reference data.

VB.NET:
<Serializable()> Public Structure ControllerType
        Public Attributes As ControllerAttributeType
        Public Description As XCData
        Public RedundancyInfo As RedundancyInfoType
        Public Security As SecurityType
        Public SafetyInfo As SafetyInfoType
        Public DataTypes() As GCCSDataType
        Public Modules() As Modules_Type
    End Structure


If I were to code as follows:
VB.NET:
Dim Controller as ControllerType
Dim TestModule as Modules_Type
TestModule = Controller.Modules(1)
TestModule.subelement1 = "test"     'subelement1 is part of Modules_Type (not shown)

This would change the value of Controller.Modules(1).subelement1 to "test" because the value of TestModule is passed by reference instead of value (not sure why this would need to be that way, but it is what it is...).

Doing some research on how to get around this, I found many references on Copying or Cloning structures.

This is the reference that seems like it does what I need.
http://www.vbdotnetforums.com/vb-net-general-discussion/5802-simple-generalized-file-i-o-deep-copy-cloning-any-object.html


As shown in the supplied code, I added <Serializable()> as a prefix to my structures. When I call this function:
VB.NET:
    Public Function CloneObject(ByVal originalObject) As Object
        Dim BinFormatter As New BinaryFormatter
        Dim memStream As New MemoryStream

        BinFormatter.Serialize(memStream, originalObject)
        memStream.Position = 0
        CloneObject = BinFormatter.Deserialize(memStream)
    End Function
I get a "SerializationException was unhandled" (Type 'System.Xml.Linq.XCData' in Assembly 'System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.) runtime error message on the BinFormatter.Serialize line.

Three questions:
1) Is the "CloneObject" function solution able to be used in my application?
2) If it is, what is the fix for the XCData typed objects?
3) If not, is there a better way?


Thanks in advance for your help. - KW
 
Can't you use String in place of XCData and store the string value there?
 
I could if there isn't another way.
I was kind of hoping for a "you just do X, Y, and Z" and that would solve the problem. Sounds like it isn't possible for that datatype.

The reason for using XCData (and XElement and XAttribute [not shown]) is that the data is originally populated with XML data. Code-wise there were some benefits in using these datatypes for reading and writing purposes.

When the program faulted out, it generated a fault on the first such datatype (XCData). I'm guessing I would see the same fault on the other similar datatypes.


If there aren't any other thoughts, I'll get to reworking it.


JohnH --- thanks again. One more time that you've set me straight.

-KW
 
Back
Top