Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

How to deserialize the values types when they are empty

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
tryokane
Asked:
tryokane
  • 3
  • 2
2 Solutions
 
angus_young_acdcCommented:
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
 
tryokaneAuthor Commented:
Can we do the same way for datetime and decimal types also?
0
 
tryokaneAuthor Commented:
I tried the above way but its not working.I am getting a message like "There was an error in the xml message".
0
 
angus_young_acdcCommented:
Where exactly are you getting the error (what code is producing it)?  As I would be just guessing without knowing more.
0
 
tryokaneAuthor Commented:
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now