N
N
NameOf Var2020-10-12 09:53:44
.NET
NameOf Var, 2020-10-12 09:53:44

How to deserialize Nullable types into XML attributes?

Hello. I have a contract like this:

[Serializable]
public class XmlContract
{
     [XmlAttribute("ImportantAttribute")]
     [DefaultValue(null)]
     public decimal? Field { get; set; }
}


Such a contract is neither serialized to XML nor deserialized from XML due to the presence of the Nullable property. Found different solutions on SO like these . I found two solutions:
1) Create a Nullable Property with an attribute , then create a non-nullable property that will set values ​​for the nullable property through setters and getters. 2) Implement the interface and describe the logic of serialization/deserialization of nullable types in it. Such solutions do not quite suit me (although they seem to work), because I have about 100-150 properties in my contract, writing such logic for each one is just tin. It will not work to replace with either, because the vendors' contract delivers exactly the contract with XML attributes.[XmlIgnore]
IXmlSerializable

[XmlAttribute][XmlElement]

I really hope that someone has already encountered such a problem, because it seems that the case is not so unique. But I already spent a lot of time on Google looking for a solution and found nothing - all in vain :(

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Korotenko, 2020-10-12
@hax

Look in my git, there just zero attributes come from fias. Now I can’t throw off the exact piece from my mobile

A
acwartz, 2020-10-12
@acwartz

Apparently they didn’t search well, or they were looking for some ready-made solutions. or
[XmlElement(IsNullable = true)]

public int? MyNullable{ get; set; }
    public bool ShouldSerializeMyNullable()
    {
        return true;
    }

J
John_Nash, 2020-10-13
@John_Nash

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

    namespace Serialization
    {
        [Serializable]
        public class Person
        {
            public string Name { get; set; }
            public int? Age { get; set; }
            public Company Company { get; set; }

            public Person()
            { }

            public Person(string name, int? age, Company comp)
            {
                Name = name;
                Age = age;
                Company = comp;
            }
        }

        [Serializable]
        public class Company
        {
            public string Name { get; set; }

            // стандартный конструктор без параметров
            public Company() { }

            public Company(string name)
            {
                Name = name;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Person person1 = new Person("Tom", 29, new Company("Microsoft"));
                Person person2 = new Person("Bill", null, new Company("Apple"));
                Person[] people = new Person[] { person1, person2 };

                XmlSerializer formatter = new XmlSerializer(typeof(Person[]));

                using (FileStream fs = new FileStream("people.xml", FileMode.OpenOrCreate))
                {
                    formatter.Serialize(fs, people);
                }

                using (FileStream fs = new FileStream("people.xml", FileMode.OpenOrCreate))
                {
                    Person[] newpeople = (Person[])formatter.Deserialize(fs);

                    foreach (Person p in newpeople)
                    {
                        Console.WriteLine($"Имя: {p.Name} --- Возраст: {p.Age?.ToString() ?? "NULL"} --- Компания: {p.Company.Name}");
                    }
                }
                Console.ReadLine();
            }
        }
    }

If some specific scheme, use the xsd utility. It must take full advantage of the XmlSerializer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question