简体   繁体   English

将多个对象序列化为XML

[英]Serialize multiple objects into XML

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. 我尝试使用以下代码将我的对象序列化为xml文件,但是运行此代码时,列表values的最后一项将按列表计数进行序列化。

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();

write.Serialize is probably overwriting whats in the file each time rather then appending. write.Serialize可能每次都覆盖文件中的内容,而不是附加内容。 You could try serializing the values to a string like in this question: Serialize an object to string . 您可以尝试将值序列化为字符串,例如以下问题: 将对象序列化为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. 或者,您可以序列化整个列表,而不是一次序列化每个值。

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. 填充列表的第一个foreach循环将反复覆盖同一value对象,并将对同一对象的新引用重复添加到列表中。 You're not actually adding a new item to your list when you call valus.Add(value); 当您调用valus.Add(value);时,您实际上并没有在列表中添加新项目valus.Add(value); .

Try moving the line values value = new values(); 尝试移动行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; 到foreach循环的顶部,或者,如果您特别担心多次声明一个对象的效率,请保留行values value; outside the loop and add the line value = new values(); 在循环外部并添加行value = new values(); inside the foreach loop. 在foreach循环中。

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: 仅供参考,如果您要添加class values列表,可以这样做:

//...
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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM