-1

i'm working on a program that I need to append objects to a xml file and read it there is no problem with writing to file my problem is with reading objects from xml file when objects being more than one when reading I have errors

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

and my main method to test : Person is a simple class just for test contain A,B,a,b fields

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

please help me

Aybab
  • 1
  • 2
  • can you also provide a sample that you are trying to append? with what as well? Can you also provide the code where you have tried to add a new Node? – Jawad Jan 23 '20 at 17:53
  • You should serialize a list of your objects. See [this](https://stackoverflow.com/questions/58686509/trouble-serializing-and-deserializing-multiple-objects/58687570#58687570) for example. –  Jan 23 '20 at 20:59

1 Answers1

0

The code below works with more than one Person. Two things you need to know about multiple item with xml

1) An array of elements are root of xml is considered "Not Well Formed Xml". The xml specification allow the array, but Net library default does not allow these array unless with XmlWriter you use the option :

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

2) Xml serializer with array creates two tags 1) Persons 2) Person. See the output of code below to see these elements.

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

}
jdweng
  • 33,250
  • 2
  • 15
  • 20