1

I try the following code to serialize my objects into xml file, but when run this code the last item in the list values serialize by the count for the list.

I want to serialize every item in the list? what is the wrong in my code

Can anyone help me ?

//list of class values               
List<values> valus = new List<values>();
values value = new values();

foreach (Control control in Controls)
{
    value.ctrlname = control.Name.ToString();
    value.ctrllocation = control.Location.ToString();
    value.ctrltext = control.Text.ToString();
    value.ctrltype = control.GetType().ToString();
    value.ctrlstatus = control.Enabled.ToString();

    valus.Add(value);    
}

System.Xml.Serialization.XmlSerializer writer =
       new System.Xml.Serialization.XmlSerializer(typeof(values));

var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//Serialization.xml";
System.IO.FileStream file = System.IO.File.Create(path);

foreach (values item in valus)
{
    writer.Serialize(file, item);
}

file.Close();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ahmed
  • 305
  • 5
  • 14

3 Answers3

0

write.Serialize is probably overwriting whats in the file each time rather then appending. You could try serializing the values to a string like in this question: Serialize an object to string. And then manually appending to the file with the string values.

Alternatively you could serialize the whole list rather then each value at a time.

Community
  • 1
  • 1
Lex Webb
  • 2,772
  • 2
  • 21
  • 36
0

Your first foreach loop that fills the list is repeatedly overwriting the same value object and adding new references to same object repeatedly to the List. You're not actually adding a new item to your list when you call valus.Add(value);.

Try moving the line values value = new values(); to the top of the foreach loop or, if you're particularly worried about efficiency of declaring an object multiple times, keep the line values value; outside the loop and add the line value = new values(); inside the foreach loop.

jcwilbur
  • 161
  • 1
  • 7
0

First lets validate that your classes are serialzble, like so:

[Serializable()]
public class rootValues
{
    public rootValues()
    {
        valuesArray = new List<values>(); 
    }

    [XmlElement("item", typeof(values))]
    public List<values> valuesList { get; set; }
} 

[Serializable()]
public class values
{
    [System.Xml.Serialization.XmlAttribute("ctrlname")]
    public string ctrlname { get; set; }

    //....
} 

Second, your using the same name for the list and object

//list of class values               
rootValues valusList = new rootValues();

foreach (Control control in Controls)
{
    values value = new values(); // Create new element, in your code it was the same

    value.ctrlname = control.Name.ToString();
    value.ctrllocation = control.Location.ToString();
    value.ctrltext = control.Text.ToString();
    value.ctrltype = control.GetType().ToString();
    value.ctrlstatus = control.Enabled.ToString();

    valuesList.valuesList.Add(value);    
}

System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(rootValues));
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//Serialization.xml";
System.IO.FileStream file = System.IO.File.Create(path);
writer.Serialize(file, valusList);
file.Close();

FYI, if you want to add a list of class values you can do it like so:

//...
List<values> SomeListOfvalues =  new List<values>(); 

// Init list
SomeListOfvalues.add(new values()); // Adding first element
SomeListOfvalues.add(new values()); // Adding second element

// Adding the whole list to our class
valuesList.valuesList.addRange(SomeListOfvalues );
Yael
  • 1,566
  • 3
  • 18
  • 25
  • what is valuesList in valuesList.Add(value); are you mean valusList ? after replaced it by valusList give me an error not contain Add method – Ahmed Sep 17 '15 at 11:33
  • about valuesList if i want to add list of values named by valuesList ? – Ahmed Sep 17 '15 at 11:37
  • just last comment how can use `valuesList.valuesList.AddRange(SomeotherValueList)` How can use that – Ahmed Sep 17 '15 at 11:42
  • Note that one important change you made in this answer from the OP's code is initializing the `value` object within the foreach loop. Without that, the code creates multiple references to the same `value` object. I think it's important to call this out. – jcwilbur Sep 17 '15 at 11:42