?
Solved

How to deserialize the values types when they are empty

Posted on 2008-10-20
5
Medium Priority
?
604 Views
Last Modified: 2013-12-17
I have simple xml file and class for that, while deserilizing the  if the xmlelement of type (int) is empty i am having problem. If I have values in the xml file it is working with no issues.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
 
namespace ConsoleApplication1
{
    [Serializable]
    public class Student
    {
        private int no;
        private string name;
 
        public int No
        {
            get { return no; }
            set { no = value; }
        }
 
        [XmlElement(IsNullable=true)]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
 
    }
}
 
<?xml version="1.0" encoding="utf-8" ?>
<Student>
	<No></No>
	<Name>Test</Name>
</Student>

Open in new window

0
Comment
Question by:tryokane
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Assisted Solution

by:angus_young_acdc
angus_young_acdc earned 400 total points
ID: 22756239
Its because you cant have type int etc as a null.  If you want that you have to explicitly say it can be set as a value of type null by using "?"  Example is in the code snippet.  That will say that the integer value can be null
    [Serializable]
    public class Student
    {
        private int? no;
        private string name;
 
        public int? No
        {
            get { return no; }
            set { no = value; }
        }
 
        [XmlElement(IsNullable = true)]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

Open in new window

0
 
LVL 2

Author Comment

by:tryokane
ID: 22763828
Can we do the same way for datetime and decimal types also?
0
 
LVL 2

Author Comment

by:tryokane
ID: 22763940
I tried the above way but its not working.I am getting a message like "There was an error in the xml message".
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 22766928
Where exactly are you getting the error (what code is producing it)?  As I would be just guessing without knowing more.
0
 
LVL 2

Accepted Solution

by:
tryokane earned 0 total points
ID: 22865631
Ok,I was able to find a solution for the above problem.The below way is the method to solve this for any type of data.Thanks for all the solutions and help offered!
 [XmlIgnore]
        public decimal ManualOverridePrice
        {
            get { return m_ManualOverridePrice; }
            set { m_ManualOverridePrice = value; }
        }
 
        [XmlElement(ElementName = "ManualOverridePrice", IsNullable = true)]
        public string ManualOverridePriceHandler
        {
            get { return m_ManualOverridePrice.ToString(); }
            set { if (value != null && value != string.Empty) m_ManualOverridePrice = XmlConvert.ToDecimal(value); }
        }

Open in new window

0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question