Question

Is there a way the below class can be serialized without the XML root being serialised, I want the serialize to start from the XMLArray?

Please let me know if you need further info.

[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; }
    }

}

No correct solution

OTHER TIPS

The XML document will need a root element of some sort. Do you just not want the root to be the customer? Then Don't mark Customer class as XmlRoot, but rather whatever is going to contain the list of customers, i.e.

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; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top