0

I have a class like this:

public class Response
{
    public String AdditionalData = "";

    public Boolean Success = false;

    public int ErrorCode = 0;

    public int WarningCode = 0;

    public Transaction TransactionInfo = null;

    public PosInfo PosInformation = null;
}

and i can serialize this successfully. but when i serialize this class 2 times and save it in a XML file,multi root error appear in XML editor. i know it needs a XML element to be root to surround other tags, but i don't know how to add root element in a serialize code. sterilizer class is below:

public class Serializer
{
   public void XMLSerializer(Response response)
    {
        string path = "D:/Serialization.xml";
        FileStream fs;
        XmlSerializer xs = new XmlSerializer(typeof(Response));
        if(!File.Exists(path))
        {
            fs = new FileStream(path, FileMode.OpenOrCreate);
        }
        else
        {
            fs = new FileStream(path, FileMode.Append);
        }
        StreamWriter sw = new StreamWriter(fs);
        XmlTextWriter xw = new XmlTextWriter(sw);
        xw.Formatting = System.Xml.Formatting.Indented;
        xs.Serialize(xw, response);
        xw.Flush();
        fs.Close();
    }
}
  • If you need to serialize multiple instances of Response, you could put all the Response-objects into a list and serialize the list. – Presi Nov 28 '18 at 08:00
  • the file modes `append` looks like it could be to blame... – jazb Nov 28 '18 at 08:16
  • The xml specification says when and array appears at the root level the Xml in "Not Well formed". It doesn't necessarily mean it is wrong. Many log files are in xml format and simply appended new log entries to the end of the log file and end up with xml that is not well formed. To read these files in Net Library you can use XmlReader and use settings : settings.ConformanceLevel = ConformanceLevel.Fragment – jdweng Nov 28 '18 at 10:11
  • If you have a file with multiple XML documents appended sequentially, you can use `ReadObjects` from [this answer](https://stackoverflow.com/a/46476652/3744182) to [Read nodes of a xml file in C#](https://stackoverflow.com/q/46476119/3744182) to read all of them into a list. – dbc Nov 28 '18 at 18:35

1 Answers1

1

I would recommend improving your code to at least take care of disposable resources.

using Statement

Provides a convenient syntax that ensures the correct use of IDisposable objects.

public class Serializer
{
    public void XMLSerializer(Response response)
    {
        string path = "D:/Serialization.xml";

        var xs = new XmlSerializer(typeof(Response));           

        using (var fs = new FileStream(path, FileMode.OpenOrCreate))
        {
            using (var sw = new StreamWriter(fs))
            {
                using (var xw = new XmlTextWriter(sw))
                {
                    xw.Formatting = System.Xml.Formatting.Indented;
                    xs.Serialize(xw, response);
                    xw.Flush();
                }
            }

            fs.Close();
        }
    }
}
Community
  • 1
  • 1
jazb
  • 5,498
  • 6
  • 37
  • 44