-1

I'm making a C# application what requires to serialize Customer data into an XML file.

So I created the following class what I want to serialize later:

public class Customer {
    private string name{get; set;}
    private int age{get; set;}

    public void setCustomerAge(){
        age = 20;
    }

    public void setCustomerName(string input){
        name = input;
    }

    public string customerName(){
        return name;
    }

    public int customerAge(){
        return age;
    }

}

The method I've tried is to create a new customer, set it's properties and call the SerializeObject() on it.

public void newCustomer(){
    Customer cust = new Customer();
    cust.setCustomerName("TEST");
    cust.setCustomerAge();
    SerializeObject(cust, savePath)
}

public void SerializeObject<T>(T serializableObject, string fileName) {
    if(serializableObject == null) {
        return;
    }

    try {
        XmlDocument xmlDocument = new XmlDocument();
        XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
        using(MemoryStream stream = new MemoryStream()) {
            serializer.Serialize(stream, serializableObject);
            stream.Position = 0;
            xmlDocument.Load(stream);
            xmlDocument.Save(fileName);
            stream.Close();
        }

    } catch(Exception ex) {
        Debug.WriteLine(ex);
    }
}

The file is generated, but the output contains these two lines only:

<?xml version="1.0"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

Am I approaching this from a wrong way and should I use a total different method?

Eyire
  • 1
  • 2
  • Side note: I've never seen anyone create java-style accessors for a private auto-property in C# like that. You should probably read up a bit on what properties are and how they're used. – Chris May 01 '16 at 17:24
  • @Fruchtzwerg Thank you for your answer, but I wish to keep that two variables private on porpuse. – Eyire May 01 '16 at 17:24
  • That's perfectly fine, but there's no point in having them be auto-properties with {get; set;} like that. Also the convention is to use PascalCasing not camelCasing in C#! – Chris May 01 '16 at 17:27
  • Aside from the {get; set;} what is really a stupidity from me. The serilaizer can get the values from the "customerName()" and "customerAge()"? – Eyire May 01 '16 at 17:29
  • The xml serializer is working on reflection on the type of the object you provide, it needs publicly accesible properties to read / write the properties, methods have nothing to do with serialization. – Janne Matikainen May 01 '16 at 17:30
  • So basically there is no way to serilaize a class like this in C#? – Eyire May 01 '16 at 17:32
  • Sure you can make your own serializer which will work on XDocument and XElements. – Janne Matikainen May 01 '16 at 17:32
  • Thank you guys for the answers! – Eyire May 01 '16 at 17:39

4 Answers4

0

You could not serialize private properties like this. A way to do this is described here. To solve your problem, you have to set your properties to public (optional private set).

public string name { get; private set; }
public int age { get; private set; }

This way, your return methodes like

public int customerAge(){
     return age;
 }

are not needed anymore. Your way to get and set the property is not very common in C# (I think this is Java-like). Look here to see how to do it in C#.

Community
  • 1
  • 1
Fruchtzwerg
  • 8,750
  • 12
  • 37
  • 44
0

From MSDN:

The Serialize method converts the public fields and read/write properties of an object into XML. It does not convert methods, indexers, private fields, or read-only properties

You've got nothing to serialize but the object itself.

Serg
  • 14,291
  • 2
  • 19
  • 40
0

The Xml.Serialization.XmlSerializer object performs only shallow serialization. If you also want to serialize the private variables of an object or child objects, you must use deep serialization. (https://support.microsoft.com/en-us/kb/815813)

So my suggestion would be to make the properties public.

Otherwise, you could check this post: Serializing private member data

Community
  • 1
  • 1
0

From the MSDN page for XmlSerializer.Serialize:

The Serialize method converts the public fields and read/write properties of an object into XML. It does not convert methods, indexers, private fields, or read-only properties. To serialize all of an object's fields and properties, both public and private, use the BinaryFormatter.

So, you need either public fields or public non-readonly properties, of which you have none.

I'd suggest:

public class Customer
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Or if you'll later need logic in the setters:

public class Customer
{
    private string m_Name;
    private int m_Age;

    public string Name { get { return m_Name; } set { m_Name = value; } }
    public int Age { get { return m_Age; } set { m_Age = value; } }
}
Chris
  • 5,257
  • 12
  • 28
  • Tank you for your answer. I just realised that I basically know nothing about the xmlserializer. – Eyire May 01 '16 at 17:38