generating xml for an object

  • Thread starter Thread starter Noor
  • Start date Start date
N

Noor

Hi all.


I need to generate xml for a class object....

I know i can generate it if i know all the fields for an object.

but i want to create a generic routice for it.. means a routine that will
take a class object and parse and extract out all the properites with the
data and write its xml representation.

how can i do this in .net

any idea? tutorial ?

regards,
Noor
 
Noor

You could use Serialization to convert the class into XML, use import
the namespace System.Xml.Serialization, then mark your class
Serializable:

<Serializable()> Public Class MyClass

Dim mText As String
Public Property Tags() As String
Get
Return mText
End Get
Set(ByVal Value As String)
mText= Value
End Set
End Property

End Class

Then to save the object:

Private Sub SaveObject()

Dim MyObject As New MyClass()

Dim Seralizer As New XmlSerializer(GetType(MyClass))
Dim XmlFile As New FileStream("C:\Temp\MyFile", _
FileMode.Create, FileAccess.Write)
Dim XmlWriter As New XmlTextWriter(XmlFile, Nothing)
Seralizer.Serialize(XmlWriter, MyObjec )
XmlWriter.Close()
XmlFile.Close()
XmlFile = Nothing

End Sub

And to load the object:

Public Sub LoadObject ()

Dim Seralizer As New XmlSerializer(GetType(MyClass))
Dim XmlFile As New FileStream("C:\Temp\MyFile", _
FileMode.Open, FileAccess.Read)
Dim XmlReader As New XmlTextReader(XmlFile)

Dim MyObject as MyClass = _
CType(Seralizer.Deserialize(XmlReader), MyClass)
XmlReader.Close()
XmlFile.Close()

End Sub


Hope this helps....

Tom
 
* "Noor said:
but i want to create a generic routice for it.. means a routine that will
take a class object and parse and extract out all the properites with the
data and write its xml representation.

Keyword: Reflection.
 
Thanks Tom ..

Im going to try this code.. will give you my feedback.
Noor
 
Yup.. it would much helpful for me if you show some code or atleast point to
some url.
I went to your website.. but it's not in english.. so cant understand
anything.

thanks
Noor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top