Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9071277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:52:13+00:00 2026-06-16T17:52:13+00:00

I use the following code to serialize xml file. I wonder whether should I

  • 0

I use the following code to serialize xml file.

I wonder whether should I use the CanDeserialize function in this case.

I noticed that CanDeserialize throws XmlException if the file is empty.

Deserialize throws InvalidOperationException in that case.

My question is should I remove that extra check and if Deserialize does some sort of check anyway?

EDIT: After reading some comments and answers, I wonder when to use CanDeserialize ?

public static T RestoreFromXml(string filename)
{
    Object res = null;
    using (var stream = new FileStream(filename, FileMode.Open))
    {
        XmlReader reader = new XmlTextReader(stream);
        try
        {
            if (xs.CanDeserialize(reader))
            {
                res = xs.Deserialize(reader);
            }
        }
        catch (XmlException ex)
        {
            throw ex;
        }            
    }
    return (T)res;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:52:14+00:00Added an answer on June 16, 2026 at 5:52 pm

    You can do some checkings before trying to deserialize – check if the file name is not null or empty, check if the file exists, etc. Here’s some code to deserialize a file:

    /// <summary>
    /// XML serializer helper class. Serializes and deserializes objects from/to XML
    /// </summary>
    /// <typeparam name="T">The type of the object to serialize/deserialize.
    /// Must have a parameterless constructor and implement <see cref="Serializable"/></typeparam>
    public class XmlSerializer<T> where T: class, new()
    {
        /// <summary>
        /// Deserializes a XML file.
        /// </summary>
        /// <param name="filename">The filename of the XML file to deserialize</param>
        /// <returns>An object of type <c>T</c></returns>
        public static T DeserializeFromFile(string filename)
        {
            return DeserializeFromFile(filename, new XmlReaderSettings());
        }
    
        /// <summary>
        /// Deserializes a XML file.
        /// </summary>
        /// <param name="filename">The filename of the XML file to deserialize</param>
        /// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlReaderSettings"/></param>
        /// <returns>An object of type <c>T</c></returns>
        public static T DeserializeFromFile(string filename, XmlReaderSettings settings)
        {
            if (string.IsNullOrEmpty(filename))
                throw new ArgumentException("filename", "XML filename cannot be null or empty");
    
            if (! File.Exists(filename))
                throw new FileNotFoundException("Cannot find XML file to deserialize", filename);
    
            // Create the stream writer with the specified encoding
            using (XmlReader reader = XmlReader.Create(filename, settings))
            {
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                return (T) xmlSerializer.Deserialize(reader);
            }
        }
    } 
    

    }

    You can use it like this:

    string filename = "...";
    
    Foo foo = XmlSerializer<Foo>.DeserializeFromFile(filename);
    

    This code was taken from an article that I wrote, you can find it here:

    XML serialization using Generics

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Normally, I use the following code to serialize object to XML file. Everyday, I
I have a timer function that serialize an object into a xml file once
So I use the following code to manipulate my 'login' form so that it
I have following code to serialize my data into a file: out = new
the following is a code sample that takes a list of file names and
I'm trying to use the following code to log users in: $(form).submit(function(event) { $.ajax({
I am trying to use this piece of code to serialize a form AND
I'm attempting to use the following code to serialize an anonymous type to JSON:
when I try to use the following code in the codebehind file, it runs
I use following code: Create a retry policy, when error, retry after 1 second,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.