serialization của tôi là như thế này ..Serialize nhiều đối tượng
public class slab
{
public int lowerlimit {get; set;}
public int upperlimit { get; set; }
public int percentage { get; set; }
}
public class Details
{
static void Main(string[] args)
{
slab s= new slab();
s.lowerlimit = 0;
s.upperlimit = 200000;
s.percentage = 0;
XmlSerializer serializer = new XmlSerializer(s.GetType());
StreamWriter writer = new StreamWriter(@"filepath");
serializer.Serialize(writer.BaseStream, s);
}
}
nó làm việc tốt và tôi nhận được tập tin đầu ra như:
<?xml version="1.0"?>
<slab xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<lowerlimit>0</lowerlimit>
<upperlimit>200000</upperlimit>
<percentage>0</percentage>
</slab>
Nhưng làm thế nào tôi có thể serialize nhiều hơn một đối tượng? Tôi muốn nhận tệp xuất dưới dạng
<slabs>
<slab>
<lowerlimit>0</lowerlimit>
<upperlimit>200000</upperlimit>
<percentage>0</percentage>
</slab>
<slab>
<lowerlimit>200000</lowerlimit>
<upperlimit>500000</upperlimit>
<percentage>10</percentage>
</slab>
<slab>
<lowerlimit>500000</lowerlimit>
<upperlimit>1000000</upperlimit>
<percentage>20</percentage>
</slab>
<slab>
<lowerlimit>1000000</lowerlimit>
<upperlimit>0</upperlimit>
<percentage>30</percentage>
</slab>
</slabs>
Đặt chúng trong một mảng và sắp đặt từng mảng ... –