0

I'm trying to so some serialization using C#, but it throws an exception saying that there was an error generating the xml document. This is where I do the serialization:

 public void serialize()
    {
        try
        {
            XmlSerializer ser = new XmlSerializer(typeof(Repository<Student>));
            StreamWriter myWriter = new StreamWriter("stud.xml");
            ser.Serialize(myWriter, rep);
            myWriter.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error " + e.Message);
        }     

    }

And this is the class I want to serialize:

 public class Repository<T> : MyStack<T>
{
    public int size;
    public int capacity;
    public SLL<T> stud;

    public Repository()
    {
        /*
         * Creator for class Repository.
         */
        this.stud = new SLL<T>();
        this.capacity = 20;
        this.size = 0;
    }

where MyStack is an interface, and SSL<T> is singly-linked list I have implemented.

1
  • 2
    What's the actual exception? Is an InnerException provided? Commented Jan 11, 2014 at 13:58

5 Answers 5

2

Please add the full details of your exception to isolate the problem. From the code that you posted, it looks like your classes are not serializable.

Are the Repository, MyStack, SSL serializable classes (do you have the [Serializable] attribute on all the types of the members that will be serialized?)

Sign up to request clarification or add additional context in comments.

2 Comments

System.TypeAccessException: Attempt by method 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterRepository1.Write4_RepositoryOfStudent(System.Object)' to access type 'NS.stackRepository.Repository'1<NS.student.Student>' failed.
Check your student class access modifier. Is it internal ? It should be public for the serializer to access it.
2

It looks odd to serialise the repository class. Instead I would serialise the SLL<T> object, since that is the actual data you are interested in. The repository is just wrapper object for getting to data, thus there's no reason to serialise that.

As Cosmin mentions, make sure you place a [Serializable] attribute on SLL<>

[Serializable]
public class SLL<T>
{
}

1 Comment

Can you let us know why it didn't work? Was there an error? Also can you post the entire code for SLL<> otherwise it's very difficult to help out.
1

Here is an example how to serialize. i think you might miss some important parts:

[Serializable]
public class User
{
    [XmlElement("login")]
    [Key]
    public string login { get; set; }

    [XmlElement("KDP")]
    public int KDP { get; set; }

    [XmlElement("attended")]
    public int attended { get; set; }
   etc.

the public getters and setters are important if you ever plan to deserialize. and here is example how to serialize list of these:

    [XmlArrayItem(typeof(User))]
    [XmlElement("usersList")]
    public static List<User> usersList = new List<User>();

        using (StreamWriter myWriter = new StreamWriter(usersPath, false))
        {
            userSerializer.Serialize(myWriter, usersList);
            myWriter.Close();
        }

hope it helps.

Comments

1

Use [XmlIgnore] attribute for the property which is causing error generating the xml document, so that other things can be parsed.

Comments

0

Generating Word with this error means it doesn’t have access to INSERT. Go to regedit:

HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Protect\Providers\df9d8cd0-1501-11d1-8c7a-00c04fc297eb

Make Dword32 key and rename it to ProtectionPolicy and set its value to 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.