Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

c# - System.ObjectDisposedException while deserializing object from xml

I am trying to deserialize object from xml file, and the code below gives me the error -

System.ObjectDisposedException: 'Cannot access closed sequence.'

public static void Main()
{
    XmlSerializer serializer = new XmlSerializer(typeof(ImportSession));
    MemoryStream stream = new MemoryStream();
    using (StreamWriter sw = new StreamWriter(stream))
    {
        sw.Write(stream);
        sw.Flush();
        stream.Position = 0;
    }
    
    foreach (string filename in Directory.EnumerateFiles(@"C:XMLFiles", "*.xml"))
    {
        ProcessFile(filename);
    }
    
    Console.ReadKey();      

        void ProcessFile(string Filename)
        {
            bool temErro = false;
            Console.WriteLine("A processar xml: " + Filename);
            XmlDocument xml = new XmlDocument();
            xml.Load(Filename);
            
            // ** error occurs in the line below **
            ImportSession session = (ImportSession)serializer.Deserialize(stream);
            foreach (Batch batch in session.Batches)
            {
                foreach (Document doc in batch.Documents)
                { 
                    foreach (Page page in doc.Pages)
                    {
                        if (!string.IsNullOrEmpty(batch.Processed.ToString()))
                        {
                            if (!string.IsNullOrEmpty(page.HasError.ToString()))
                            {
                                string Import = page.ImportFileName;
                                Console.WriteLine("Página com erro:" + Import);
                                temErro = true;
                            }
                        }
                    }
                }
            }            

            if (temErro)
                Console.WriteLine("Ficheiro com erro: " + Filename);
            else
                Console.WriteLine("Ficheiro processado: " + Filename);

            Console.WriteLine(Filename);
        }
    }
}

Following are other model classes used in deserialization operation -

public class ImportSession
{
    public Batch[] Batches { get; set; }
}

public class Batch
{
    [XmlAttribute]
    public string Name { get; set; }
    [XmlAttribute]
    public string Description { get; set; }
    [XmlAttribute]
    public string BatchClassName { get; set; }
    [XmlAttribute]
    public bool Processed { get; set; }
    public Document[] Documents { get; set; }
}

public class Document
{
    [XmlAttribute]
    public string FormTypeName { get; set; }
    public IndexField[] IndexFields { get; set; }
    public Page[] Pages { get; set; }
}

public class IndexField
{
    [XmlAttribute]
    public string Name { get; set; }
    [XmlAttribute]
    public string Value { get; set; }
}

public class Page
{
    [XmlAttribute]
    public string ImportFileName { get; set; }
    [XmlAttribute]
    public string ErrorCode { get; set; }
    [XmlAttribute]
    public string ErrorMessage { get; set; }
    [XmlIgnore]
    public bool HasError => !string.IsNullOrWhiteSpace(ErrorMessage);
}
question from:https://stackoverflow.com/questions/65882421/system-objectdisposedexception-while-deserializing-object-from-xml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are writing empty MemoryStream stream into StreamWriter that uses it.

That's the reason why nothing is actully written


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...