1

I am trying to serializing a XML file using the following code,

using System;
using System.Xml;
using System.Xml.Serialization;

namespace TestXML
{
    [Serializable]
    [XmlRootAttribute("Test")]
    public class Test100
    {
        [XmlElementAttribute("StartDate")]
        public DateTime StartDate { get; set; }

        [XmlElementAttribute("EndDate")]
        public DateTime EndDate { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test100 obj = new Test100();
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Test100));
                XmlReader reader = XmlReader.Create(@"C:\MyProjects\TestXML\TestXML\Test.xml");
                obj = (Test100)serializer.Deserialize(reader);
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

The XML file:

<?xml version="1.0"?>
<Test>
    <StartDate>2020-01-19T00:00:00Z</StartDate>
    <EndDate></EndDate>
</Test>

Exception : The string '' is not a valid AllXsd value.

Thanks in advance for your help.

Aleksandr Ivanov
  • 2,778
  • 5
  • 27
  • 35
Ullan
  • 905
  • 4
  • 15
  • 28
  • Can you please share a full [mcve] that reproduces the problem, including XML and compilable classes? But it's probably a duplicate of [XmlSerializer: The string '' is not a valid AllXsd value](https://stackoverflow.com/q/2861779/3744182) and/or [The string '3/18/09 10:16 PM' is not a valid AllXsd value](https://stackoverflow.com/q/661881/3744182), and/or [XML Deserialization of a date with an empty value](https://stackoverflow.com/q/838246/3744182). – dbc Jan 28 '20 at 22:51
  • Thanks, I will update with the code soon. – Ullan Jan 28 '20 at 22:56
  • `DateTime?` doesn't work because null values are represented by an `xsi:nil` value not an empty value, see [Deserializing XML null dates to DateTime in C#](https://stackoverflow.com/q/46162195/3744182). – dbc Jan 28 '20 at 22:56
  • *I can fix this issue by changing the data type DateTime to string, then I will end up with doing string to date conversion in many places.* - Nothing has changed with `XmlSerializer` since those older questions were answered I'm afraid. Using "shim" string properties may be the only way to go. – dbc Jan 28 '20 at 22:59
  • Thanks for your comments: https://csharp.wekeepcoding.com/article/23268444/XmlSerializer%3A+The+string+''+is+not+a+valid+AllXsd+value – Ullan Jan 28 '20 at 23:32
  • What should it do? What `DateTime` value do you expect for an element with no value? – Ondrej Tucny Jan 29 '20 at 12:09
  • @OndrejTucny, the element can be null. – Ullan Jan 29 '20 at 14:17

1 Answers1

0

I updated the code as below

public string EndDate { get; set; }

[XmlIgnore]
public bool? _EndDate
{
    get
    {
        if (!string.IsNullOrWhiteSpace(EndDate))
        {
           return bool.Parse(EndDate);
        }
           return null;
    }
}

The above code is handling the null issue.

Ullan
  • 905
  • 4
  • 15
  • 28