Click here to Skip to main content
14,734,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
given the following code 
[Serializable]
    public class check
    {
        public check()
        {
            //int_array = new List<int>();
            string_array = new List<string>();
        }

        [XmlArray("string_array",Order=1)]
        [XmlArrayItem("string_element",typeof(string))]
        public List<string> string_array;

        //[XmlArray("int_array")]
        //[XmlArrayItem("int_element")]
        //public List<int> int_array;
        public void write_to_xml(string path)
        {
            XmlSerializer serailizer = new XmlSerializer(typeof(check));
            TextWriter writer = new StreamWriter(path);
            serailizer.Serialize(writer, this);
            writer.Close();
        }
        static void Main()
        {
           //check class
            check c = new check();
            c.string_array.AddRange(new string[] { "string1", "string2", "string3" });
            c.write_to_xml(@"E:\check.xml");
        }
    }


this code run OK and XML file generated is
<?xml version="1.0" encoding="utf-8"?>
<check xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <string_array>
    <string_element>string1</string_element>
    <string_element>string2</string_element>
    <string_element>string3</string_element>
  </string_array>
</check>

now remove comments in previous class and hit F5 again
what happen
this error appear There was an error reflecting type 'FET.check'.
Why this error appear?
IS serialized class must contain only one instance of XmlArray attribute ? or what?
Posted

1 solution

Hello Ibrahim,

Just change the annotations on int List as shown below.
[XmlArray("int_array", Order=2)]
[XmlArrayItem("int_element", typeof(int))]
public List<int> int_array;</int>


Regards,
   
Comments
ibrahim ragab 24-Mar-13 10:12am
   
Thanks very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900