Results 1 to 14 of 14

Thread: XML in .Net?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Most of the work I do with XML is through the DataSet Object, maybe for user settings, small amounts of data, or whatever.

  3. #3
    New Member
    Join Date
    Sep 2002
    Location
    Hoesbach, Bayern, Germany
    Posts
    6

    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?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  5. #5
    New Member
    Join Date
    Sep 2002
    Location
    Hoesbach, Bayern, Germany
    Posts
    6
    I want to use it as a File format.
    I want to save 4 Bitmap with some extra data (2 collections with ~20 Objects)

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  7. #7
    New Member
    Join Date
    Sep 2002
    Location
    Hoesbach, Bayern, Germany
    Posts
    6
    ok, and how can i do this???

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

    thanks, funkycoder

  8. #8

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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?

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'here are the imports
    2. Imports System.IO
    3. Imports System.Runtime.Serialization
    4. Imports System.Runtime.Serialization.Formatters.Binary
    5.  
    6.  
    7. 'Code for a click event or somethign in a form or whatever
    8.         'make test object and put somethign in it
    9.         Dim obj As New MyFileFormat()
    10.         obj.MyData = "Testing"
    11.  
    12.         'serialize it to a file
    13.         Dim fs As FileStream = New FileStream(Path.Combine(Application.StartupPath, "test.mff"), FileMode.OpenOrCreate)
    14.         Dim bf As New BinaryFormatter()
    15.         bf.Serialize(fs, obj)
    16.         bf = Nothing
    17.         fs.Close()
    18.         fs = Nothing
    19.  
    20.         'get it back from a file
    21.         fs = New FileStream(Path.Combine(Application.StartupPath, "test.mff"), FileMode.OpenOrCreate)
    22.         bf = New BinaryFormatter()
    23.  
    24.         'make new object to hold it
    25.         Dim obj2 As MyFileFormat
    26.         obj2 = bf.Deserialize(fs)
    27.         bf = Nothing
    28.         fs.Close()
    29.         fs = Nothing
    30.  
    31.         'show the results
    32.         MsgBox(obj2.MyData)
    33.  
    34.  
    35.  
    36. 'sample class that is serializable
    37. <Serializable()> _
    38.  Public Class MyFileFormat
    39.     'Public MyObjects As New Collection()
    40.     Public MyImages(4) As Image
    41.     Public MyData As String
    42. End Class

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

  11. #11

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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:
    1. Import System.Xml.Serialization
    2.  
    3. Class Ser
    4.   Dim tc As theclass = New theclass("Mickey","Mouse")
    5.   Dim ser As XmlSerializer = New XmlSerializer(GetType(theclass))
    6.  
    7.   ser.Serialize(Console.out, the class)
    8. End Class
    9.  
    10. Class theclass
    11.   Public fname As String
    12.   Public lname As String
    13.  
    14.   Public Sub New()
    15.  
    16.   End Sub
    17.  
    18. Public Sub New(ByVal fname As String, ByVal lname As String)
    19.      Me.fname  = fname
    20.      Me.lname  = lname    
    21.      
    22.   End Sub
    23. 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:
    1. Class theclass
    2.   <XmlElementAttribute("Vorname")>Public fname As String
    3.    Public fname As String
    4.   <XmlElementAttribute("Zuname")>Public lname As String
    5.   Public lname As String
    6.  
    7.  
    8.   Public Sub New()
    9.  
    10.   End Sub
    11.  
    12.  Public Sub New(ByVal fname As String, ByVal lname As String)
    13.      Me.fname  = fname
    14.      Me.lname  = lname
    15.  
    16.   End Sub
    17. 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)

  12. #12

  13. #13
    New Member
    Join Date
    Sep 2002
    Location
    Hoesbach, Bayern, Germany
    Posts
    6

    Thumbs up 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!

    ~

  14. #14

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width