|
-
Aug 31st, 2002, 07:15 PM
#1
Thread Starter
Dazed Member
XML in .Net?
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.
-
Aug 31st, 2002, 11:30 PM
#2
Most of the work I do with XML is through the DataSet Object, maybe for user settings, small amounts of data, or whatever.
-
Sep 1st, 2002, 04:26 AM
#3
New Member
XML with the Dataset
HI
which method to open xml-files is easier to use:
- with Dataset
- with System.XML
- with MSXML 3
is there another way?
-
Sep 1st, 2002, 04:32 AM
#4
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.
-
Sep 1st, 2002, 04:40 AM
#5
New Member
I want to use it as a File format.
I want to save 4 Bitmap with some extra data (2 collections with ~20 Objects)
-
Sep 1st, 2002, 04:48 AM
#6
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.
-
Sep 1st, 2002, 09:20 AM
#7
New Member
ok, and how can i do this???
(could you give me a little code-snippet...?)
thanks, funkycoder
-
Sep 1st, 2002, 04:10 PM
#8
Thread Starter
Dazed Member
Hey Edneeis here is another question. If you don't mind. When changing the format of XML do you use serialization with source code style attributes or do you just opt to use XSLT?
-
Sep 1st, 2002, 05:01 PM
#9
Originally posted by Dilenger4
Hey Edneeis here is another question. If you don't mind. 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.
-
Sep 1st, 2002, 05:17 PM
#10
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):
VB Code:
'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.
-
Sep 1st, 2002, 05:27 PM
#11
Thread Starter
Dazed Member
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.
VB Code:
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.
VB Code:
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)
Last edited by Dilenger4; Sep 2nd, 2002 at 01:19 AM.
-
Sep 2nd, 2002, 01:22 AM
#12
Thread Starter
Dazed Member
Any luck funkycoder? Edneeis does raise an interesting point. What is in the collection?
-
Sep 4th, 2002, 04:00 AM
#13
New Member
Yo! It runs!
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!
~
-
Sep 4th, 2002, 12:44 PM
#14
Thread Starter
Dazed Member
Yeah im not sure which way would be best but at least you got it working.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|