PDA

Click to See Complete Forum and Search --> : XML in .Net?


Dillinger4
Aug 31st, 2002, 07:15 PM
This is a question for anyone that uses XML at their job. Does most of the XML related programming you do involve serializing classes to XML or creating XML documents programmatically using XMLWriters? Thanks. :)

Edneeis
Aug 31st, 2002, 11:30 PM
Most of the work I do with XML is through the DataSet Object, maybe for user settings, small amounts of data, or whatever.

funkycoder
Sep 1st, 2002, 04:26 AM
HI

which method to open xml-files is easier to use:

- with Dataset
- with System.XML
- with MSXML 3

is there another way? :confused:

Edneeis
Sep 1st, 2002, 04:32 AM
I guess it depends whats in the XML file or what you want to do with it. The good part about using the dataset is it loads the XML file right in and then you have it as tables and rows which is easy to work with. If you already know the schema then you can make a typed dataset and even use the intellisense to help you with the structure.

funkycoder
Sep 1st, 2002, 04:40 AM
I want to use it as a File format.
I want to save 4 Bitmap with some extra data (2 collections with ~20 Objects)

Edneeis
Sep 1st, 2002, 04:48 AM
For that you could just make a class and serialize it to a file. Although the collection may give you trouble unless all the items are serializable.

funkycoder
Sep 1st, 2002, 09:20 AM
ok, and how can i do this???

(could you give me a little code-snippet...?)

thanks, funkycoder

Dillinger4
Sep 1st, 2002, 04:10 PM
Hey Edneeis here is another question. If you don't mind. :D When changing the format of XML do you use serialization with source code style attributes or do you just opt to use XSLT?

Edneeis
Sep 1st, 2002, 05:01 PM
Originally posted by Dilenger4
Hey Edneeis here is another question. If you don't mind. :D When changing the format of XML do you use serialization with source code style attributes or do you just opt to use XSLT?

I'm sorry I'm not sure what you mean. If there is a decent chance that the structure of the XML will change then I'd probably just load the xml file into an untyped dataset. Altough it would take a bit of work for the program to then make use of the new stuff. An untyped dataset just uses the generic tables() rows() arrays or collections so your code would still work but if you change the format then the data might be in the wrong spot. If you use a typed dataset then it actually has the properties from the XLS including attributes and stuff. I guess it really depends on what you are trying to accomplish.

Edneeis
Sep 1st, 2002, 05:17 PM
As for funkycoder, I was just trying and it doesn't look like you can easily serialize a collection since it may contain objects that are not serializable. Whats in the collection?

Here is some code to show you serialization (one way anyway):

'here are the imports
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary


'Code for a click event or somethign in a form or whatever
'make test object and put somethign in it
Dim obj As New MyFileFormat()
obj.MyData = "Testing"

'serialize it to a file
Dim fs As FileStream = New FileStream(Path.Combine(Application.StartupPath, "test.mff"), FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter()
bf.Serialize(fs, obj)
bf = Nothing
fs.Close()
fs = Nothing

'get it back from a file
fs = New FileStream(Path.Combine(Application.StartupPath, "test.mff"), FileMode.OpenOrCreate)
bf = New BinaryFormatter()

'make new object to hold it
Dim obj2 As MyFileFormat
obj2 = bf.Deserialize(fs)
bf = Nothing
fs.Close()
fs = Nothing

'show the results
MsgBox(obj2.MyData)



'sample class that is serializable
<Serializable()> _
Public Class MyFileFormat
'Public MyObjects As New Collection()
Public MyImages(4) As Image
Public MyData As String
End Class


That will at least get you started if you choose that route.

Dillinger4
Sep 1st, 2002, 05:27 PM
I was just actualy reading an article on using soure code style attributes vs XSLT and i started to wonder what is most commonly used on the job. Probably bolth though. For instance say that i wanted to serialize a class as XML.

Import System.Xml.Serialization

Class Ser
Dim tc As theclass = New theclass("Mickey","Mouse")
Dim ser As XmlSerializer = New XmlSerializer(GetType(theclass))

ser.Serialize(Console.out, the class)
End Class

Class theclass
Public fname As String
Public lname As String

Public Sub New()

End Sub

Public Sub New(ByVal fname As String, ByVal lname As String)
Me.fname = fname
Me.lname = lname

End Sub
End Class

But now say that i wanted to change the elements to conform to a german standard. We could use the source code style attribute
XmlElementAttribute like the following.


Class theclass
<XmlElementAttribute("Vorname")>Public fname As String
Public fname As String
<XmlElementAttribute("Zuname")>Public lname As String
Public lname As String


Public Sub New()

End Sub

Public Sub New(ByVal fname As String, ByVal lname As String)
Me.fname = fname
Me.lname = lname

End Sub
End Class

Im pretty sure this is how it could be done but i have to look into using XSLT further since i think that is the main purpose if XSLT(to change the underlying Xml structure) :)

Dillinger4
Sep 2nd, 2002, 01:22 AM
Any luck funkycoder? Edneeis does raise an interesting point. What is in the collection? :)

funkycoder
Sep 4th, 2002, 04:00 AM
Hi

I've tried both ways - Binary Serialization and XML Serialization and it works both perfect!
But I don't know, which I should use.

The Binary Files are smaller, but when someone opens a file and deletes a space-char the file is "destoried"

The XML-File is more secure, but when I save a (binary) Bitmap-file within it would become extremely big!

~

Dillinger4
Sep 4th, 2002, 12:44 PM
Yeah im not sure which way would be best but at least you got it working. :D