简体   繁体   English

System.ObjectDisposedException 从 xml 反序列化 object

[英]System.ObjectDisposedException while deserializing object from xml

I am trying to deserialize object from xml file, and the code below gives me the error -我正在尝试从 xml 文件反序列化 object ,下面的代码给了我错误 -

System.ObjectDisposedException: 'Cannot access closed sequence.' System.ObjectDisposedException:“无法访问已关闭的序列。”

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 -以下是反序列化操作中使用的其他 model 类 -

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 -我正在尝试从 xml 文件反序列化 object ,下面的代码给了我错误 -

System.ObjectDisposedException: 'Cannot access closed sequence.' System.ObjectDisposedException:“无法访问已关闭的序列。”

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 -以下是反序列化操作中使用的其他 model 类 -

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 -我正在尝试从 xml 文件反序列化 object ,下面的代码给了我错误 -

System.ObjectDisposedException: 'Cannot access closed sequence.' System.ObjectDisposedException:“无法访问已关闭的序列。”

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 -以下是反序列化操作中使用的其他 model 类 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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