Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / September 2007

Tip: Looking for answers? Try searching our database.

How to save/load a Type in Xml

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Juan Dent - 25 Sep 2007 22:36 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.