xml序列化产生异常 [英] xml serialization produce exception

查看:47
本文介绍了xml序列化产生异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生!

i有xml序列化的代码,但它产生无效的操作异常。

代码如下:

sir !
i have code for xml serialization but it producing invalid operation exception.
The code is below:

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();
        }
    }
}

推荐答案

完整代码:



complete code:

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


这篇关于xml序列化产生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆