在 C# Dot Net 中,当您想要反序列化 xml 文件时如何处理异常?但默认情况下该文件不存在

发布于 2025-01-20 21:05:50 字数 999 浏览 0 评论 0原文

在C#DOT NET中,当您要去除XML文件时如何处理异常,但是默认情况下,该文件不存在!因为您必须运行该程序才能创建一个程序。

以下是我需要帮助的领域。

public static Compare_Data[] Deserialize()
        {
            Compare_Data[] cm;
            cm = null;
            string path = @"C:\Users\XYZ\Desktop\BACKUP_DATA\log.xml";
            XmlSerializer xs = new XmlSerializer(typeof(Compare_Data[]));

            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    // This will read the XML from the file and create the new instance of Compare_Data.
                    cm = (Compare_Data[])xs.Deserialize(fs);
                    return cm;
                } 
            }
            else
            {
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    xs.Serialize(fs); ///  what to add here ?
                }
            }

            return null;
        }

In C# Dot Net, How to handle a exception when you want to de-serialize a xml file, but by default the file doesn't exists! because you have to run the program to create one.

Below is the area where I need Help.

public static Compare_Data[] Deserialize()
        {
            Compare_Data[] cm;
            cm = null;
            string path = @"C:\Users\XYZ\Desktop\BACKUP_DATA\log.xml";
            XmlSerializer xs = new XmlSerializer(typeof(Compare_Data[]));

            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    // This will read the XML from the file and create the new instance of Compare_Data.
                    cm = (Compare_Data[])xs.Deserialize(fs);
                    return cm;
                } 
            }
            else
            {
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    xs.Serialize(fs); ///  what to add here ?
                }
            }

            return null;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酒忠诚 2025-01-27 21:05:50

如果一般,您不希望方法具有副作用。在这种情况下,在else分支中创建一个空日志文件可能是不必要的,当要记录数据时,应通过单独的serialize()方法来处理。

您的代码可以简化这样的内容:

public static Compare_Data[] Deserialize()
{
    const string path = @"C:\Users\XYZ\Desktop\BACKUP_DATA\log.xml";

    if (!File.Exists(path)) 
    {
        // return null or an empty array, depending on how 
        // you want the calling code to handle this.
        return null;
    }

    using (FileStream fs = new FileStream(path, FileMode.Open))
    {
        var xs = new XmlSerializer(typeof(Compare_Data[]));
        return (Compare_Data[])xs.Deserialize(fs);
    } 
}

If general, you don't want your methods to have side effects. In this case, creating an empty log file in the else branch is probably unnecessary and should be handled by a separate Serialize() method when there is data to be logged.

Your code could be simplified something like this:

public static Compare_Data[] Deserialize()
{
    const string path = @"C:\Users\XYZ\Desktop\BACKUP_DATA\log.xml";

    if (!File.Exists(path)) 
    {
        // return null or an empty array, depending on how 
        // you want the calling code to handle this.
        return null;
    }

    using (FileStream fs = new FileStream(path, FileMode.Open))
    {
        var xs = new XmlSerializer(typeof(Compare_Data[]));
        return (Compare_Data[])xs.Deserialize(fs);
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文