开发者

Serialize PrinterSettings gives exception

开发者 https://www.devze.com 2023-01-04 11:22 出处:网络
I am trying to persist the PrinterSettings (VB.NET 3.5), but keep getting exceptions reading back in.Here is the code I am using.Works fine o开发者_如何学JAVAn other types of objects.

I am trying to persist the PrinterSettings (VB.NET 3.5), but keep getting exceptions reading back in. Here is the code I am using. Works fine o开发者_如何学JAVAn other types of objects.

I have tried changing the default printer to a different printer, also used Microsoft XPS Document Writer.

I read that .NET 1.1 had a problem serializing printersettings, but was corrected in 2.0.

I am sure it is something dumb I am doing...

Dim p = New Printing.PrinterSettings
    Dim L_PrintPageSettings = New System.Drawing.Printing.PageSettings
    p = L_PrintPageSettings.PrinterSettings
    Dim objStreamWriter As New StreamWriter("C:\Product.xml")
    Dim x As New XmlSerializer(p.GetType)
    x.Serialize(objStreamWriter, p)
    objStreamWriter.Close()
    Try
        'Deserialize text file to a new object.
        Dim objStreamReader As New StreamReader("C:\Product.xml")
        Dim p2 As New Printing.PrinterSettings
        p2 = x.Deserialize(objStreamReader)
        objStreamReader.Close()
    Catch ex As Exception

    End Try


The class isn't IXmlSerializable. Try leveraging the fact that it is annotated with the Serializable attribute: http://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings(VS.71).aspx

Use either a BinaryFormatter, or a NetDataContractSerializer or a DataContractSerializer (note that the last one will only work in .NET 3.5 SP1).

The last one will produce the most human readable output so you'd probably be best off using that.

See example for DataContractSerializer at the bottom of this article:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

A google search will show how to use the BinaryFormatter or NetDataContractSerializer if you're not already familiar.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号