Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 20th, 2004, 03:28 AM
Authorized User
 
Join Date: Sep 2004
Location: stuttgart, , Germany.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to write abstract class to xml

Hi all,

I am coding in VB.NET and here I must use abstract class in my project. When I write to xml file in serialization, there was an error generating the XML document. The following is the code:

Public Class Form1
    Inherits System.Windows.Forms.Form
    Public c4 As New Class4()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim c1 As New Class1()
        c1.a = "2"
        c1.b = " 3"
        Dim c2 As New Class2()
        c1.cc = c2
        c2.m = "4"
        c4.items.Add(c1)
        c4.save()
    End Sub
 End Class

Public Class Class1
    Public a As String
    Public b As String
    Public cc As Class3
 End Class

Public Class Class2
    Inherits Class3
    Public m As String
End Class


Imports System.Xml.Serialization
Imports System.IO
Public Class Class4
    <XmlIgnore()> Public items As New ArrayList()
    Public Property c1() As Class1()
        Get
            Dim c1array(items.Count - 1) As Class1
            items.CopyTo(c1array)
            Return c1array
        End Get
        Set(ByVal Value As Class1())
             items.Clear()
            If Not Value Is Nothing Then
                Dim c1 As Class1
                For Each c1 In Value
                    items.Add(c1)
                Next
            End If
        End Set
    End Property

    Public Sub save()
        Dim ser As New XmlSerializer(Me.GetType)
        Dim writer As New StreamWriter("c:\file.xml")
'error information appeared in this statement
        ser.Serialize(writer, Me)
        writer.Close()
    End Sub
End Class

Thanks a lot in advance,
Haiying




Reply With Quote
  #2 (permalink)  
Old September 20th, 2004, 08:27 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

I don't think you can serialize in that way, I think you need the live object and serialize that. So like the function would be in the form, and the form would serialize the class.

Brian
Reply With Quote
  #3 (permalink)  
Old September 21st, 2004, 04:09 AM
Authorized User
 
Join Date: Sep 2004
Location: stuttgart, , Germany.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Brian,

I tried to serialize the live object, but it still does not work. The error information is as same as before. I guess the problem is there is an abstract class inside. Then I used SoapFormatter class, the object can be written into xml file, but the format is almost unreadable.

Haiying

Reply With Quote
  #4 (permalink)  
Old September 21st, 2004, 07:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

What is the error message, and could the problem be with <XmlIgnore()>?

Brian
Reply With Quote
  #5 (permalink)  
Old September 21st, 2004, 08:32 AM
Authorized User
 
Join Date: Sep 2004
Location: stuttgart, , Germany.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey,

The error message is "there was an error generating the XML document".I have add <XmlIgnore()> to some variables which are not useful. If I add <XmlIgnore()> in front of the abstract class variable in class1, the error disappeared.

Haiying


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about abstract class nf0001384 C# 2005 5 July 22nd, 2013 06:58 AM
Why abstract class can't be instantiated? anisurr C++ Programming 5 July 23rd, 2008 01:32 AM
xml invalid top level from ASP write XML(solution) g000we XML 0 August 9th, 2006 03:56 AM
interface and abstract class sreenu.pocha C# 2 July 26th, 2006 07:18 AM
question about abstract class/polymorphism csharplearner C# 4 October 11th, 2005 03:27 PM



All times are GMT -4. The time now is 07:07 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.