有没有办法在没有序列化XML根的情况下序列化下面的类,我希望序列化从XMLArray开始?
如果您需要更多信息,请与我们联系。
[XMLRoot]
public class Customers
{
List<Customer> _Customers = new List<Customer>();
[XmlArray("Customers")]
[XmlArrayItem("Customer")]
public List<Customer> Customers
{
get { return _Customers; }
set { _Customers = value; }
}
}
public class Customer
{
string _test1;
string _test2;
public string test1
{
get { return _test1; }
set { _test1 = value; }
}
public string test2
{
get { return _test2; }
set { _test2 = value; }
}
}
答案 0 :(得分:0)
XML文档需要某种根元素。你不想让root成为客户吗?然后不要将Customer类标记为XmlRoot,而是将包含客户列表的任何内容标记为,
class Program
{
static void Main(string[] args)
{
Shop shop = new Shop()
{
Name = "Jack's Shop",
Customers = new List<Customer>()
{
new Customer() { FirstName = "Maynard", LastName = "Keating" },
}
};
XmlSerializer xmls = new XmlSerializer(typeof(Shop));
using (FileStream fs = File.Create("JacksShop.xml"))
xmls.Serialize(fs, shop);
}
}
[XmlRoot]
public class Shop
{
public string Name { get; set; }
public List<Customer> Customers { get; set; }
}
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
}