Serializing Lists of Classes to XML

前端 未结 4 1945
清歌不尽
清歌不尽 2020-12-01 03:44

I have a collection of classes that I want to serialize out to an XML file. It looks something like this:

public class Foo
{
  public List BarList         


        
相关标签:
4条回答
  • 2020-12-01 04:00
    var xmlfromLINQ = new XElement("BarList",
                from c in BarList 
                select new XElement("Bar",
                    new XElement("Property1", c.Property1),
                    new XElement("Property2", c.Property2)
                 ));
    
    0 讨论(0)
  • 2020-12-01 04:06

    Just to check, have you marked Bar as [Serializable]?

    Also, you need a parameter-less ctor on Bar, to deserialize

    Hmm, I used:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
    
            Foo f = new Foo();
    
            f.BarList = new List<Bar>();
    
            f.BarList.Add(new Bar { Property1 = "abc", Property2 = "def" });
    
            XmlSerializer ser = new XmlSerializer(typeof(Foo));
    
            using (FileStream fs = new FileStream(@"c:\sertest.xml", FileMode.Create))
            {
                ser.Serialize(fs, f);
            }
        }
    }
    
    public class Foo
    {
        [XmlArray("BarList"), XmlArrayItem(typeof(Bar), ElementName = "Bar")]
        public List<Bar> BarList { get; set; }
    }
    
    [XmlRoot("Foo")]
    public class Bar
    {
        public string Property1 { get; set; }
        public string Property2 { get; set; }
    }
    

    And that produced:

    <?xml version="1.0"?>
    <Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <BarList>
        <Bar>
          <Property1>abc</Property1>
          <Property2>def</Property2>
        </Bar>
      </BarList>
    </Foo>
    
    0 讨论(0)
  • 2020-12-01 04:13

    Everything looks great. As @Carl said you need to add the [Serializable] attibute to your classes, but other than that your XML creation should work find.

    Foo

    [Serializable]
    [XmlRoot("Foo")]
    public class Foo
    {
        [XmlArray("BarList"), XmlArrayItem(typeof(Bar), ElementName = "Bar")]
        public List<Bar> BarList { get; set; }
    }
    

    Bar

    [Serializable]
    public class Bar
    {
        public string Property1 { get; set; }
        public string Property2 { get; set; }
    }
    

    Code to test

    Foo f = new Foo();
    f.BarList = new List<Bar>();
    f.BarList.Add(new Bar() { Property1 = "s", Property2 = "2" });
    f.BarList.Add(new Bar() { Property1 = "s", Property2 = "2" });
    
    FileStream fs = new FileStream("c:\\test.xml", FileMode.OpenOrCreate);
    System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(Foo));
    s.Serialize(fs, f);
    

    Output

    <?xml version="1.0" ?> 
    <Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <BarList>
            <Bar>
                <Property1>s</Property1> 
                <Property2>2</Property2> 
            </Bar>
            <Bar>
                <Property1>s</Property1> 
                <Property2>2</Property2> 
            </Bar>
        </BarList>
    </Foo>
    
    0 讨论(0)
  • 2020-12-01 04:25

    It has been over 5 years since this item was posted. I give my experience from July 2013 (.NET Framework 4.5). For what it's worth and to whom it may concern:

    When I define a class like so: (VB.Net code)

    <Serializable> Public Class MyClass
        Public Property Children as List(of ChildCLass)
        <XmlAttribute> Public Property MyFirstProperty as string
        <XmlAttribute> Public Property MySecondProperty as string
    End Class
    
    <Serializable> Public Class ChildClass
        <XmlAttribute> Public Property MyFirstProperty as string
        <XmlAttribute> Public Property MySecondProperty as string
    End Class
    

    With this definition the class is (de)serialized without any problems. This is the XML that comes out of here:

    <MyClass> MyFirstProperty="" MySecondProperty=""
        <Children>
            <ChildClass> MyFirstProperty="" MySecondProperty=""
            </ChildClass>
       </Children>
    </MyClass>
    

    It only took me two days to figure it out that the solution was to leave out the <XmlElement> prefix of the List(of T) elements.

    0 讨论(0)
提交回复
热议问题