7 Replies - 6366 Views - Last Post: 18 August 2010 - 07:57 AM Rate Topic: -----

#1 Birken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-August 10

Convert structure to XML file

Posted 17 August 2010 - 03:39 AM

Hello Forum,

is there any way to convert a structure to a XML file? I searched in this forum and in Internet but only found conversions from classes to XML files. I also found a working conversion (or better serialization) from structure to binary file but this is not what I want. My structure looks like this:
Public Structure MyStruct
 Dim bFraming As Boolean
 Dim CtrlBus As Integer
 Dim RS232 As IO.Ports.SerialPort 
End Structure

Public newStruct As New MyStruct



I used a code snippet (from conversion class to XML) and changed it to convert this structure to a XML file ---> but not working:
        Dim objSerializer As System.Xml.Serialization.XmlSerializer
        Dim objStream As System.IO.FileStream
        objStream = _
           New System.IO.FileStream(Application.StartupPath & "\test.xml", _
           IO.FileMode.OpenOrCreate)
        objSerializer = New System.Xml.Serialization.XmlSerializer(newStruct.GetType)

        objSerializer.Serialize(objStream, newStruct)
        objStream.Close()



Line6 gives an error message: System.InvalidOperationException in System.Xml.dll

Try - catch gives an Exception (translated to english...):
Failed to reflect type 'System.IO.Ports.SerialPort'. --->
System.InvalidOperationException() : the(Element) 'System.ComponentModel.Component.Site' of Type 'System.ComponentModel.ISite' can not be serialized.


Maybe someone can help... I dont know how to proceed
Birken

This post has been edited by Birken: 17 August 2010 - 06:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Convert structure to XML file

#2 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Convert structure to XML file

Posted 17 August 2010 - 05:33 AM

Not all types are serializable. Instead of using a SerialPort object, create a structure/class that contains the properties of the serial port(BaudRate,Parity,DataBits,StopBits etc).
Was This Post Helpful? 0
  • +
  • -

#3 Birken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-August 10

Re: Convert structure to XML file

Posted 17 August 2010 - 06:03 AM

This could be difficult (or time-consuming...) because SerialPort object contains ~30 properties.

Attached Image

Or is there a simple method to convert this object (with all properties at once) to a class/structure?
And this new class/structure still has to be in my other structure "newStruct".

This post has been edited by Birken: 17 August 2010 - 06:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 Birken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-August 10

Re: Convert structure to XML file

Posted 17 August 2010 - 06:36 AM

I could also assign RS232 (Serial Port object) not to be serialized, but it still does not work:

 
Public Structure MyStruct  
  Dim bFraming As Boolean  
  Dim CtrlBus As Integer  
   <NonSerialized()> Dim RS232 As IO.Ports.SerialPort   
 End Structure 




Still exception in line 6 (first post)....
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: Convert structure to XML file

Posted 17 August 2010 - 07:46 AM

You could add a method/extension method to manually serialize it. I typically use the System.Xml.Linq objects, so I'd make a ToXElement method or something like it. Then just manually create the element and assign child elements or attributes. It's not the most elegant of solutions but it will get the job done.
Was This Post Helpful? 1
  • +
  • -

#6 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Convert structure to XML file

Posted 17 August 2010 - 08:19 AM

You don't need all the properties, you only need to save the ones you used to create it in the first place.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Convert structure to XML file

Posted 17 August 2010 - 03:23 PM

Objects that can't be serialised are generally found to have a hardware aspect to them.
Was This Post Helpful? 0
  • +
  • -

#8 Birken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-August 10

Re: Convert structure to XML file

Posted 18 August 2010 - 07:57 AM

For your interests...

Because Interfaces canīt be serialized I changed my structure to:
Public Structure MyStruct    
Dim bFraming As Boolean    
Dim CtrlBus As Integer    
<Xml.Serialization.XmlIgnore()> Dim RS232 As IO.Ports.SerialPort     
End Structure 



This prevents error in line 6 (see first post). Then I added manually the most interesting properties from RS232 object to my xml file.
Not perfect but working...

thanks for help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1