将XML反序列化为对象

时间:2020-01-23 17:48:35

标签: c# xml xmlserializer

我正在研究一个程序,我需要将对象追加到xml文件并读取它,写入文件没有问题,我的问题是从xml文件中读取对象,而读取时对象不止一个错误

        public static void WriteToXmlFile<T>(string filePath, T objectToWrite, bool append = false) where T : new()
    {
        TextWriter writer = null;
        try
        {
            var serializer = new XmlSerializer(typeof(T));
            writer = new StreamWriter(filePath, append);
            serializer.Serialize(writer, objectToWrite);
        }
        finally
        {
            if (writer != null)
                writer.Close();
        }
    }


        public static T ReadFromXmlFile<T>(string filePath) where T : new()
    {
        TextReader reader = null;
        try
        {
            var serializer = new XmlSerializer(typeof(T));
            reader = new StreamReader(filePath);
            Console.WriteLine("file readed correctly");
            return (T)serializer.Deserialize(reader);
        }

        finally
        {
            if (reader != null)
                reader.Close();
        }
    }

和我主要的测试方法: Person是一个简单的类,仅用于测试,包含A,B,a,b字段

    static void Main(string[] args)
    {

        Person p1 = new Person();
        p1.A = 1;
        p1.B = 2;
        Person p2 = new Person();
        p2.A = 45;
        p2.B = 65;
        Person p3 = new Person();
        p3.A = 213;
        p3.B = 34;
        Person p4 = new Person();
        p4.A = 45;
        p4.B = 234;
        Person p5 = new Person();
        p5.A = 324;
        p5.B = 123;
        Person p6 = new Person();
        p6.A = 53;
        p6.B = 53;
        Person p7 = new Person();
        p7.A = 46545;
        p7.B = 6435;
        Person p8 = new Person();
        p8.A = 4355;
        p8.B = 6435;
        Person p9 = new Person();
        p9.A = 4455;
        p9.B = 6455;
        Person p10 = new Person();
        p10.A = 4455;
        p10.B = 6345;



        Person[] per = new Person[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 };

        foreach (Person pppp in per)
        {
            FileIO.WriteToXmlFile<Person>("C://Users//ulduz//Desktop//ShoppingBackend//ShoppingBackend//personList.xml", pppp, true);
        }



        foreach (Person pppp in per)
        {
            Console.WriteLine(FileIO.ReadXML<Person>("C://Users//ulduz//Desktop//ShoppingBackend//ShoppingBackend//personList.xml").A);
        }

请帮助我

1 个答案:

答案 0 :(得分:0)

以下代码可与多个人一起使用。您需要了解关于xml的多个项目的两件事

1)元素数组是xml的根,被认为是“格式不正确的Xml”。 xml规范允许该数组,但是Net库默认不允许这些数组,除非使用XmlWriter使用以下选项:

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);

2)具有数组的Xml序列化程序创建两个标签1)人员2)人员。请参阅下面的代码输出以查看这些元素。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Person p1 = new Person() { A = 1, B = 2};
            Person p2 = new Person() { A = 45, B = 65 };
            Person p3 = new Person() { A = 213, B = 34 };
            Person p4 = new Person() { A = 45, B = 234 };
            Person p5 = new Person() { A = 324, B = 123 };
            Person p6 = new Person() { A = 53, B = 53 };
            Person p7 = new Person() { A = 46545, B = 6435 };
            Person p8 = new Person() { A = 4355, B = 6435 };
            Person p9 = new Person() { A = 4455, B = 6455 };
            Person p10 = new Person() { A = 4455, B = 6345 };

            List<Person> people = new List<Person>() { p1, p2, p3, p4, p5, p6, p7, p8, p9, p10 };

            WriteToXmlFile(FILENAME, people);

        }
        public static void WriteToXmlFile<T>(string filePath, T objectToWrite, bool append = false) where T : new()
        {
            TextWriter writer = null;
            try
            {
                var serializer = new XmlSerializer(typeof(T));
                writer = new StreamWriter(filePath, append);
                serializer.Serialize(writer, objectToWrite);
            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }
        }


        public static T ReadFromXmlFile<T>(string filePath) where T : new()
        {
            TextReader reader = null;
            try
            {
                var serializer = new XmlSerializer(typeof(T));
                reader = new StreamReader(filePath);
                Console.WriteLine("file readed correctly");
                return (T)serializer.Deserialize(reader);
            }

            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }
    }
    public class Person
    {
        public int A { get; set; }
        public int B { get; set; }
    }

}