Hi,
I am using XmlWriter/XmlReader to save.load classes. However, one of the
members of one class is of type Type. How can I write and then read that as
Xml?

Signature
Thanks in advance,
Juan Dent, M.Sc.
WenYuan Wang [MSFT] - 26 Sep 2007 10:57 GMT
Hello Juan,
According to your description, what you need is to serialize/deserialize
Type Member as XML.
Please correct me if I misunderstood anything here.
My suggest is that we can store the FullName of type in the XML file, and
use getType() method to retrive type from XML file when deserializing.
I wrote a sample as below. Hope this helps.
================Main============================
Sub Main()
Dim a As TestType = New TestType("TestName",
GetType(System.DateTime))
Dim sr As System.Xml.Serialization.XmlSerializer = New
System.Xml.Serialization.XmlSerializer(GetType(TestType))
Dim xw As System.Xml.XmlWriter =
System.Xml.XmlWriter.Create("test.xml")
sr.Serialize(xw, a)
xw.Close()
Dim xr As System.Xml.XmlReader =
System.Xml.XmlReader.Create("test.xml")
Dim e As TestType = sr.Deserialize(xr)
End Sub
================TestType Class===================
Public Class TestType
Implements IXmlSerializable
Private _value As String
Private _type As Type
Public Sub New()
End Sub
Public Sub New(ByVal value As String, ByVal type As Type)
Me._value = value
Me._type = type
End Sub
Public Function GetSchema() As _
System.Xml.Schema.XmlSchema _
Implements IXmlSerializable.GetSchema
Throw New System.NotImplementedException()
End Function
Public Sub WriteXml(ByVal writer As XmlWriter) _
Implements IXmlSerializable.WriteXml
writer.WriteStartElement _
("testType", "urn:devx-com")
writer.WriteAttributeString _
("value", _value)
writer.WriteAttributeString _
("type", _type.FullName)
writer.WriteEndElement()
End Sub
Public Sub ReadXml(ByVal reader As XmlReader) _
Implements IXmlSerializable.ReadXml
Dim nt As XmlNodeType = _
reader.MoveToContent()
While reader.Read()
If ((nt = XmlNodeType.Element) _
And (reader.LocalName = "testType")) Then
Me._value = reader("value")
Me._type = Type.GetType(reader("type"))
End If
End While
End Sub
Public Overrides Function ToString() As String
Return (String.Format("TestType [{0} {1}]"))
End Function
End Class
Please let me know if this is what you need. I will follow up. We are glad
to assist you.
Have a great day.
Best regards,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
WenYuan Wang [MSFT] - 28 Sep 2007 10:16 GMT
Hello Juan,
This is Wen Yuan again. I haven't heard from your a couple of days. Have
you resolved the issue so far?
If the issue still persists, please feel free to update here again. We are
glad to assist you.
Have a great day,
Best regards,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.