Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2012 Serialize object to XML

$
0
0
Here is my code to serialize an object to XML:

Code:

    Public Sub SerializeObjectToXML(FileName As String, ObjectToSerialize As Object, ObjectType As Type)
        Dim Serializer As New XmlSerializer(ObjectType)
        Dim Writer As New StreamWriter(FileName)
        Serializer.Serialize(Writer, ObjectToSerialize)
        Writer.Close()
    End Sub

I know that I can apply the <XmlIgnore()> attribute to prevent XML serialization of the attribute but can code be done with this attribute?

Here is my situation:

I have many objects that I wish to serialize to XML. Each object has a Locked Boolean attribute and I am wanting to write some code such that if an object's Locked attribute is True, the whole object will not be serialized.

This is easy with a single object, just check the Locked attribute before object serialization, however, some objects have a List of another type of object. In this situation, can I prevent any of the List objects that are Locked to not be serialized?

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>