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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:37:55+00:00 2026-05-23T02:37:55+00:00

I have problem with deserialize document to object using XmlSerializer class. Code my function

  • 0

I have problem with deserialize document to object using XmlSerializer class.

Code my function for deserialize:

   static public TYPE xmlToObject<TYPE>( string xmlDoc ) {
        MemoryStream stream = new MemoryStream();
        byte[] xmlObject = ASCIIEncoding.ASCII.GetBytes( xmlDoc );
        stream.Write( xmlObject, 0, xmlObject.Length );
        stream.Position = 0;

        TYPE message;

        XmlSerializer xmlSerializer = new XmlSerializer( typeof( TYPE ) );

        try {
            message = (TYPE)xmlSerializer.Deserialize( stream );
        } catch ( Exception e ) {
            message = default( TYPE );
        } finally {
            stream.Close();
        }
        return message;
    }

And I have class:

    public class Test {
           public int a;
           public int b;
    }

And deserialize:

    string text = File.ReadAllText( "blue1.xml" );
    Test a = XmlInterpreter.xmlToObject<Test>( text );

Ok, when I read file like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <Test>
       <a>2</a>
       <b>5</b>
    </Test> 

everything is OK. But like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <Test>
        <a>2</a>
        <b></b>
    </Test> 

is wrong because

    <b></b> 

is empty and conversion into int is impossible.

How can I solve this? For example I want in this context that b will be not declared.

What when my class is:

    public class Test {

        public enum Pro {
             VALUE1,
             VALUE2
        }

        public Pro p1;
   }

And I want accept xmlDocument, where field p1 is empty.

  • 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-05-23T02:37:56+00:00Added an answer on May 23, 2026 at 2:37 am

    I expect that first example is just some type because it has empty b as well. First of all not every XML can be deserialized to object. Especially empty elements are dangerous and should not be used. If you want to express that b is not defined then do not include it in XML file:

    <?xml version="1.0" encoding="UTF-8"?>
    <Test>
        <a>2</a>
    </Test> 
    

    or make your b property nullable:

    public class Test {
           public int a;
           public int? b;
    }
    

    and define XML as:

    <?xml version="1.0" encoding="UTF-8"?>
    <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <a>2</a>
        <b xsi:nil="true" />
    </Test> 
    

    Generally if you want to use deserialization try to first use serialization to understand how must a valid XML look like.

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

Sidebar

Related Questions

I have problem converting from string to date. The first one i Ok: Date
I have a problem after I convert List<MyCustomObject> to a HashSet<MyCustomObject> . When I
I am trying to Deserialize an XML file into a few class objects: Artist,
I have problem with my own Subversion repository. It worked yesterday. But I received
I have problem dealing with EditItemTemplate. What I am trying to do is to
i have problem with my map. Map was working correctly about 5 months ago.
i have problem with jqgrid headers. in firefox it is displaying properly as shown
Problem I need a key-value store that can store values of the following form:
One of the nastiest problems working across time zones :) Here is my problem:

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.