Question38018419 xsi:ni with other attributes

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;

    public partial class location
    {
        bool nil;

        [XmlAttribute("nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public bool Nil { 
            get 
            {
                return nil;
            } 
            set 
            {
                nil = value;
            } 
        }

        public bool ShouldSerializeNil() { return nil == true; }
    }

    public partial class location
    {
        private string cityField;

        private string countryField;

        private string stateField;

        private string textField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string city
        {
            get
            {
                return this.cityField;
            }
            set
            {
                this.cityField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string country
        {
            get
            {
                return this.countryField;
            }
            set
            {
                this.countryField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string state
        {
            get
            {
                return this.stateField;
            }
            set
            {
                this.stateField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Text
        {
            get
            {
                return this.textField;
            }
            set
            {
                this.textField = value;
            }
        }
    }

    [System.Xml.Serialization.XmlRootAttribute("locations", Namespace = "")]
    public class LocationList
    {
        [XmlElement("location", IsNullable = false)] // Manually changed to false
        public List<location> Locations { get; set; }
    }

    [System.Xml.Serialization.XmlRootAttribute("locations", Namespace = "")]
    public class LocationListLocationsIsNullableTrue
    {
        [XmlElement("location", IsNullable = true)] // When left as true, all xsi:nil="true" entries are deserialized to null
        public List<location> Locations { get; set; }
    }

    public static class XmlSerializationHelper
    {
        public static T LoadFromXML<T>(this string xmlString)
        {
            using (StringReader reader = new StringReader(xmlString))
            {
                return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
            }
        }

        public static string GetXml<T>(this T obj, bool omitStandardNamespaces = false)
        {
            XmlSerializerNamespaces ns = null;
            if (omitStandardNamespaces)
            {
                ns = new XmlSerializerNamespaces();
                ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
            }			
            using (var textWriter = new StringWriter())
            {
                var settings = new XmlWriterSettings() { Indent = true, IndentChars = "    " }; // For cosmetic purposes.
                using (var xmlWriter = XmlWriter.Create(textWriter, settings))
                    new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
                return textWriter.ToString();
            }
        }
    }

	public class Program
	{
        static string GetXml()
        {
            var xml = @"<locations xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"">
    <location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
    <location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
    <location country=""BRAZIL"" city=""Passo Fundo"" state=""Rio Grande do Sul"" xsi:nil=""true""/>
    <location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
    <location country=""BRAZIL"" city=""Passo Fundo"" state=""Rio Grande do Sul"" xsi:nil=""true""/>
    <location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
    <location country=""PARAGUAY"" city=""Ciudad del Este"" state=""Alto Parana""/>
</locations>";
            return xml;
        }
		
		static void Test<T>(string xml)
		{
            var root = xml.LoadFromXML<T>();

            var xml2 = root.GetXml(false);

			Console.WriteLine(string.Format("Deserialized and re-serialized {0} XML: ", typeof(T)));
			Console.WriteLine(xml2);
			Console.WriteLine("");
		}
		
		public static void Main()
		{
            var xml = GetXml();

			Console.WriteLine("Original XML: ");
			Console.WriteLine(xml);
			Console.WriteLine("");

			Test<LocationList>(xml);
			
			Test<LocationListLocationsIsNullableTrue>(xml);
		}
	}
;