Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
sir !
i have code for xml serialization but it producing invalid operation exception.
The code is below:
C#
public class employee
    {
        public string name;
    }
    public class company
    {
        [XmlAttribute]
        public string comapnyname;
        [XmlAttribute]
        public XmlAttribute[] xattributes;
        [XmlAnyElement]
        public XmlElement[] xelements;
        [XmlElement(ElementName = "Members")]
        public employee[] employee;

        public void serializexml()
        {
            string filename = "D:\\xmlserialization.xml";
            XmlSerializer xmlserial = new XmlSerializer(typeof(company));
            TextWriter textwrite = new StreamWriter(filename);
            company com = new company();
            com.comapnyname = "hello world ltd";
            employee emp = new employee();
            employee emp1 = new employee();
            emp.name = "tushar";
            emp1.name = "mayank";
            com.employee = new employee[2]
            {
                emp,emp1};
            xmlserial.Serialize(textwrite, com);
            textwrite.Close();
            FileStream fs = new FileStream(filename, FileMode.Open);
            company cmp = (company)xmlserial.Deserialize(fs);
            Console.WriteLine(cmp.comapnyname);
            foreach (employee e in cmp.employee)
            {
                Console.WriteLine("{0}", e.name);
            }
        }
        public static void Main(String[] ar)
        {
            company co = new company();
            co.serializexml();
            Console.ReadLine();
        }
    }
}
Posted
Updated 30-Jan-14 22:24pm
v2
Comments
Bernhard Hiller 31-Jan-14 2:50am    
in which line is that exception raised?
tusharkaushik 31-Jan-14 7:30am    
the exeption raised in this line:
XmlSerializer xmlserial = new XmlSerializer(typeof(company));

1 solution

complete code:

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Test_XmlSerialize
{
    [Serializable]
    public class Employee
    {
        public string Name { get; set; }
    }

    [Serializable]
    public class Company
    {
        public string CompName { get; set; }
        public List<Employee> Employees { get; set; }
        public override string ToString()
        {
            string s = string.Empty;
            s += this.CompName + Environment.NewLine;
            s += "Employees:" + Environment.NewLine;
            foreach (var e in this.Employees)
            {
                s += e.Name + Environment.NewLine;
            }
            return s;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Company comp = new Company()
                {
                    CompName = "comp1",
                    Employees = new List<Employee>()
                };
                comp.Employees.Add(new Employee() { Name = "emp1" });
                comp.Employees.Add(new Employee() { Name = "emp2" });

                using (StreamWriter sw = new StreamWriter(@"d:\tmp\1.txt"))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(Company),
                        new Type[] { typeof(Employee) });
                    ser.Serialize(sw, comp);
                }

                using (StreamReader sr = new StreamReader(@"d:\tmp\1.txt"))
                {
                    XmlSerializer ser = new XmlSerializer(typeof(Company),
                        new Type[] { typeof(Employee) });
                    Company c2 = ser.Deserialize(sr) as Company;
                    if (c2 != null)
                        Console.WriteLine(c2);
                };
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
 
Share this answer
 
v4
Comments
tusharkaushik 1-Feb-14 5:34am    
Can u explained me in brief sir!
please!
Vedat Ozan Oner 1-Feb-14 5:50am    
When you serialize an object with the XmlSerializer, its class and sub-classes should be marked with [Serializable] attribute. I will update the solution.
tusharkaushik 1-Feb-14 9:31am    
can u modiffy this code!
i cant explain ! i h've done but no result
tusharkaushik 3-Feb-14 13:37pm    
thnx u so much!
Vedat Ozan Oner 4-Feb-14 5:11am    
you are welcome :)

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