简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM