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 8653287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:30:33+00:00 2026-06-12T14:30:33+00:00

I have an XML file that I need to deserialize into an object similar

  • 0

I have an XML file that I need to deserialize into an object similar to this one:

public class TestObject
{
    public string Name { get; set; }
    public int Size { get; set; }

    public TestObject()
    {
        Name = string.Empty;
        Size = 0;
    }
}

My deserialize method looks like this:

private TestObject DeserializeConfiguration(string xmlFileName)
{
    XmlSerializer deserializer = new XmlSerializer(typeof(TestObject));
    TextReader textReader = new StreamReader(xmlFileName);
    TestObject testObj = (TestObject)deserializer.Deserialize(textReader);
    textReader.Close();
    return testObj;
}

This is working well enough for me but on occasion, I get an XML file that may contain an invalid data type (by “invalid”, I mean with respect to the type of the object property that it should map to). For example, if my XML file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<TestObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>Orion</Name>
  <Size>abc</Size>
</TestObject>

Obviously I can’t convert “abc” into the integer Size property of my object. When I attempt to deserialize this, I see an InvalidOperationException and, not surprisingly, the InnerException is “Input string was not in a correct format”. Is it possible to catch this error, use a default value for that property of my object and continue deserializing the remainder of the XML file? If not, can anyone tell me if there’s a generally regarded “best practice” for handling invalid data during deserialization?

  • 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-12T14:30:35+00:00Added an answer on June 12, 2026 at 2:30 pm

    What you would need to do is to validate the incoming XML before deserializing. Basically you want to avoid having to process badly-formed XML. After validation the deserializer can at least be sure that all incoming XML will be deserializable.

    You can create an XML Schema that contains the definition of valid XML in your case, and then validate the incoming XML with the XSD (XML Schema Definition) first (see also http://www.codeguru.com/csharp/csharp/cs_data/xml/article.php/c6737/Validation-of-XML-with-XSD.htm for more details).

    Good luck!

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

Sidebar

Related Questions

I have an xml file that I need to load. This xml file holds
I have the following xml file that I need to read into my C#
I have this Xml file, and I need to Deserialize it back to a
I have a dynamic products xml file that i need to load from an
I have an XML file consisting of Name/Value pairs that I need in a
I have an XML file that reads like this <abc> <ab>value</ab> <aa>time</aa> <ac>money</ac> </abc>
I have a XML file that I need to populate multiple SQL tables, and
I have an xml file that looks like this: ... <e1> <e2> <e3>content1.1</e3> <e3>content1.2</e3>
I have an XML file that I need to transform using an XSL script.
I have an XML file that specifies the image files that I need to

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.