Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to: Read Object Data from an XML File in Visual Basic 

This example reads object data that was previously written to an XML file using the XmlSerializer class.

Example

This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in XML. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).

Public Class Book
    Public Title As String
    Public Sub New()
    End Sub
End Class

Public Sub ReadXML()
    Dim reader As New System.Xml.Serialization.XmlSerializer(GetType(Book))
    Dim file As New System.IO.StreamReader("c:\IntroToVB.xml")
    Dim introToVB As Book
    introToVB = CType(reader.Deserialize(file), Book)
End Sub

Compiling the Code

Replace the file name "c:\IntroToVB.xml" with the name of the file containing the serialized data. For more information about serializing data, see How to: Write Object Data to an XML File in Visual Basic.

The class must have a public constructor without parameters.

Only public properties and fields are deserialized.

Robust Programming

The following conditions may cause an exception:

  • The class being serialized does not have a public, parameterless constructor.

  • The data in the file does not represent data from the class to be deserialized.

  • The file does not exist (IOException).

Security

Always verify inputs, and never deserialize data from an untrusted source. The re-created object runs on a local computer with the permissions of the code that deserialized it. Verify all inputs before using the data in your application.

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft