locked
Deserialize Xml Object [C#] RRS feed

  • Question

  • Hello guys. I am working in an project and I want to deserialize a huge xml file from api. The problem is this:

    I have this XML Snippet:

    <effectiveTime xsi:type="IVL_TS">
       <low nullFlavor="UNK"/>
       <high nullFlavor="UNK"/>
    </effectiveTime>
    
    <effectiveTime operator="A" xsi:type="PIVL_TS">
       <period unit="h" value="12"/>
    </effectiveTime>

    As you can see is the same but those have different xsi:type attribute.

    Well I create two classes to deserialize those.

        [XmlType(TypeName = "IVL_TS")]
        public class effectiveTime
        {
            [XmlElement("low")]
            public low low { get; set; }
            [XmlElement("high")]
            public high high { get; set; }
        }
    
        [XmlType(TypeName = "PIVL_TS")]
        public class _effectiveTime
        {
            [XmlAttribute(AttributeName = "operator")]
            public string _operator { get; set; }
            [XmlElement]
            public period period { get; set; }
        }

     Here is the problem i tried something like this:

    [XmlArrayItem(Type = typeof(effectiveTime))]
    [XmlArrayItem(Type = typeof(_effectiveTime))]
    public object[] effectiveTime { get; set; }

    But when I deserialize the object the count of array is 0. What can i do?

    Best regards.

    Wednesday, August 8, 2018 12:12 PM

Answers

  • Use it as a start. And just adjust the type selector attributes:

    namespace ConsoleCS
    {
        using System.Xml.Serialization;
    
        public class root
        {
            [XmlArrayItem("effectiveTime")]
            [XmlArrayItem(typeof(EffectiveTime_IVL_TS))]
            [XmlArrayItem(typeof(EffectiveTime_PIVL_TS))]
            public BaseEffectiveTime[] effectiveTimes { get; set; }
        }
    
        [XmlType("IVL_TS")]
        public class EffectiveTime_IVL_TS : BaseEffectiveTime
        {
            public Low low { get; set; }
            public High high { get; set; }
        }
        [XmlType("PIVL_TS")]
        public class EffectiveTime_PIVL_TS : BaseEffectiveTime
        {
            public Period period { get; set; }
            [XmlAttribute()]
            public string @operator { get; set; }
        }
        public abstract class BaseEffectiveTime { }
    
        public class Period
        {
            [XmlAttribute()]
            public string unit { get; set; }
            [XmlAttribute()]
            public byte value { get; set; }
        }
    
        public class Low : NullFlavor { }
        public class High : NullFlavor { }
        public abstract class NullFlavor
        {
            [XmlAttribute()]
            public string nullFlavor { get; set; }
        }
    }

    as

    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml.Serialization;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                root root;
                XmlSerializer serializer = new XmlSerializer(typeof(root));
                using (TextReader reader = new StringReader(RetrieveXml()))
                {
                    root = (root)serializer.Deserialize(reader);
                }
    
                Console.WriteLine(root.effectiveTimes.Length);
                Console.WriteLine(root.effectiveTimes[0].GetType().Name);
                Console.WriteLine(root.effectiveTimes[1].GetType().Name);
                Console.WriteLine((root.effectiveTimes[0] as EffectiveTime_IVL_TS).low.nullFlavor);
                Console.WriteLine((root.effectiveTimes[0] as EffectiveTime_IVL_TS).high.nullFlavor);
                Console.WriteLine((root.effectiveTimes[1] as EffectiveTime_PIVL_TS).@operator);
                Console.WriteLine((root.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.unit);
                Console.WriteLine((root.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.value);
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private static string RetrieveXml()
            {
                return @"
                <root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                    <effectiveTimes>
    	                <effectiveTime xsi:type=""IVL_TS"">
    	                   <low nullFlavor=""UNK""/>
    	                   <high nullFlavor=""NI""/>
    	                </effectiveTime>
    	                <effectiveTime operator=""A"" xsi:type=""PIVL_TS"">
    	                   <period unit=""h"" value=""12""/>
    	                </effectiveTime>
                    </effectiveTimes>
                </root>
                ";
            }
        }
    }



    Wednesday, August 8, 2018 2:18 PM
  • And? Did you apply it to your XML structure? What is the problem?

    How could I have known this? Can you imagine that posting complete samples is necessary? Especially when namespaces are involved?

    You need elements instead of an array:

    namespace ConsoleCS
    {
        using System.Xml.Serialization;
    
        public class root
        {
            public SubstanceAdministration substanceAdministration { get; set; }        
        }
    
        public class SubstanceAdministration
        {
            [XmlAttribute()]
            public string classCode { get; set; }
            [XmlAttribute()]
            public string moodCode { get; set; }
            [XmlElement("templateId")]
            public TemplateId[] templateId { get; set; }
            public ID id { get; set; }
            public StatusCode statusCode { get; set; }        
            [XmlElement("effectiveTime")]
            [XmlElement(typeof(EffectiveTime_IVL_TS))]
            [XmlElement(typeof(EffectiveTime_PIVL_TS))]
            public BaseEffectiveTime[] effectiveTimes { get; set; }
        }
    
        [XmlType("IVL_TS")]
        public class EffectiveTime_IVL_TS : BaseEffectiveTime
        {
            public Low low { get; set; }
            public High high { get; set; }
        }
        [XmlType("PIVL_TS")]
        public class EffectiveTime_PIVL_TS : BaseEffectiveTime
        {
            public Period period { get; set; }
            [XmlAttribute()]
            public string @operator { get; set; }
        }
        public abstract class BaseEffectiveTime { }
    
        public class Period
        {
            [XmlAttribute()]
            public string unit { get; set; }
            [XmlAttribute()]
            public byte value { get; set; }
        }
    
        public class Low : NullFlavor { }
        public class High : NullFlavor { }
        public abstract class NullFlavor
        {
            [XmlAttribute()]
            public string nullFlavor { get; set; }
        }
    
        public class TemplateId
        {
            [XmlAttribute()]
            public string root { get; set; }
        }
    
        public class ID
        {
            [XmlAttribute()]
            public uint extension { get; set; }
            [XmlAttribute()]
            public string root { get; set; }
        }
    
        public class StatusCode
        {
            [XmlAttribute()]
            public string code { get; set; }
        }
    }
    
    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml.Serialization;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                root root;
                XmlSerializer serializer = new XmlSerializer(typeof(root));
                using (TextReader reader = new StringReader(RetrieveXml()))
                {
                    root = (root)serializer.Deserialize(reader);
                }
    
                Console.WriteLine(root.substanceAdministration.templateId[0].root);
                if (root.substanceAdministration.effectiveTimes != null)
                {
                    Console.WriteLine(root.substanceAdministration.effectiveTimes.Length);
                    Console.WriteLine(root.substanceAdministration.effectiveTimes[0].GetType().Name);
                    Console.WriteLine(root.substanceAdministration.effectiveTimes[1].GetType().Name);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[0] as EffectiveTime_IVL_TS).low.nullFlavor);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[0] as EffectiveTime_IVL_TS).high.nullFlavor);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[1] as EffectiveTime_PIVL_TS).@operator);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.unit);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.value);
                }
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private static string RetrieveXml()
            {
                return @"
                <root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                    <substanceAdministration classCode=""SBADM"" moodCode=""INT"">
    	                <templateId root=""2.16.840.1.113883.10.20.1.24""/>
    	                <templateId root=""1.3.6.1.4.1.12559.11.10.1.3.1.3.2""/>
    	                <id extension=""21957777"" root=""1.21.1""/>
    	                <statusCode code=""active""/>
    	                <effectiveTime xsi:type=""IVL_TS"">
    		                <low nullFlavor=""UNK""/>
    		                <high nullFlavor=""UNK""/>
    	                </effectiveTime>
    	                <effectiveTime operator=""A"" xsi:type=""PIVL_TS"">
    		                <period unit=""h"" value=""12""/>
    	                </effectiveTime>
                    </substanceAdministration>
                </root>
                ";
            }
        }
    }


    Wednesday, August 8, 2018 4:25 PM

All replies

  • hmm, what kind of API? Cause doing it manually maybe not necessary.

    Otherwise, use Paste Special (requires that you have installed WCF): 

    Just paste a complete XML fragment into a class file.

    Wednesday, August 8, 2018 1:24 PM
  • First try it was this. But not working well. It is a CDA Xml.
    Wednesday, August 8, 2018 1:37 PM
  • Use it as a start. And just adjust the type selector attributes:

    namespace ConsoleCS
    {
        using System.Xml.Serialization;
    
        public class root
        {
            [XmlArrayItem("effectiveTime")]
            [XmlArrayItem(typeof(EffectiveTime_IVL_TS))]
            [XmlArrayItem(typeof(EffectiveTime_PIVL_TS))]
            public BaseEffectiveTime[] effectiveTimes { get; set; }
        }
    
        [XmlType("IVL_TS")]
        public class EffectiveTime_IVL_TS : BaseEffectiveTime
        {
            public Low low { get; set; }
            public High high { get; set; }
        }
        [XmlType("PIVL_TS")]
        public class EffectiveTime_PIVL_TS : BaseEffectiveTime
        {
            public Period period { get; set; }
            [XmlAttribute()]
            public string @operator { get; set; }
        }
        public abstract class BaseEffectiveTime { }
    
        public class Period
        {
            [XmlAttribute()]
            public string unit { get; set; }
            [XmlAttribute()]
            public byte value { get; set; }
        }
    
        public class Low : NullFlavor { }
        public class High : NullFlavor { }
        public abstract class NullFlavor
        {
            [XmlAttribute()]
            public string nullFlavor { get; set; }
        }
    }

    as

    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml.Serialization;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                root root;
                XmlSerializer serializer = new XmlSerializer(typeof(root));
                using (TextReader reader = new StringReader(RetrieveXml()))
                {
                    root = (root)serializer.Deserialize(reader);
                }
    
                Console.WriteLine(root.effectiveTimes.Length);
                Console.WriteLine(root.effectiveTimes[0].GetType().Name);
                Console.WriteLine(root.effectiveTimes[1].GetType().Name);
                Console.WriteLine((root.effectiveTimes[0] as EffectiveTime_IVL_TS).low.nullFlavor);
                Console.WriteLine((root.effectiveTimes[0] as EffectiveTime_IVL_TS).high.nullFlavor);
                Console.WriteLine((root.effectiveTimes[1] as EffectiveTime_PIVL_TS).@operator);
                Console.WriteLine((root.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.unit);
                Console.WriteLine((root.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.value);
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private static string RetrieveXml()
            {
                return @"
                <root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                    <effectiveTimes>
    	                <effectiveTime xsi:type=""IVL_TS"">
    	                   <low nullFlavor=""UNK""/>
    	                   <high nullFlavor=""NI""/>
    	                </effectiveTime>
    	                <effectiveTime operator=""A"" xsi:type=""PIVL_TS"">
    	                   <period unit=""h"" value=""12""/>
    	                </effectiveTime>
                    </effectiveTimes>
                </root>
                ";
            }
        }
    }



    Wednesday, August 8, 2018 2:18 PM
  • Use it as a start. And just adjust the type selector attributes:

    namespace ConsoleCS
    {
        using System.Xml.Serialization;
    
        public class root
        {
            [XmlArrayItem("effectiveTime")]
            [XmlArrayItem(typeof(EffectiveTime_IVL_TS))]
            [XmlArrayItem(typeof(EffectiveTime_PIVL_TS))]
            public BaseEffectiveTime[] effectiveTimes { get; set; }
        }
    
        [XmlType("IVL_TS")]
        public class EffectiveTime_IVL_TS : BaseEffectiveTime
        {
            public Low low { get; set; }
            public High high { get; set; }
        }
        [XmlType("PIVL_TS")]
        public class EffectiveTime_PIVL_TS : BaseEffectiveTime
        {
            public Period period { get; set; }
            [XmlAttribute()]
            public string @operator { get; set; }
        }
        public abstract class BaseEffectiveTime { }
    
        public class Period
        {
            [XmlAttribute()]
            public string unit { get; set; }
            [XmlAttribute()]
            public byte value { get; set; }
        }
    
        public class Low : NullFlavor { }
        public class High : NullFlavor { }
        public abstract class NullFlavor
        {
            [XmlAttribute()]
            public string nullFlavor { get; set; }
        }
    }

    as

    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml.Serialization;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                root root;
                XmlSerializer serializer = new XmlSerializer(typeof(root));
                using (TextReader reader = new StringReader(RetrieveXml()))
                {
                    root = (root)serializer.Deserialize(reader);
                }
    
                Console.WriteLine(root.effectiveTimes.Length);
                Console.WriteLine(root.effectiveTimes[0].GetType().Name);
                Console.WriteLine(root.effectiveTimes[1].GetType().Name);
                Console.WriteLine((root.effectiveTimes[0] as EffectiveTime_IVL_TS).low.nullFlavor);
                Console.WriteLine((root.effectiveTimes[0] as EffectiveTime_IVL_TS).high.nullFlavor);
                Console.WriteLine((root.effectiveTimes[1] as EffectiveTime_PIVL_TS).@operator);
                Console.WriteLine((root.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.unit);
                Console.WriteLine((root.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.value);
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private static string RetrieveXml()
            {
                return @"
                <root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                    <effectiveTimes>
    	                <effectiveTime xsi:type=""IVL_TS"">
    	                   <low nullFlavor=""UNK""/>
    	                   <high nullFlavor=""NI""/>
    	                </effectiveTime>
    	                <effectiveTime operator=""A"" xsi:type=""PIVL_TS"">
    	                   <period unit=""h"" value=""12""/>
    	                </effectiveTime>
                    </effectiveTimes>
                </root>
                ";
            }
        }
    }



    Yes but xml is from api.

                <substanceAdministration classCode="SBADM" moodCode="INT">
                  <templateId root="2.16.840.1.113883.10.20.1.24"/>
                  <templateId root="1.3.6.1.4.1.12559.11.10.1.3.1.3.2"/>
                  <id extension="21957777" root="1.21.1"/>
                  <statusCode code="active"/>
                  <effectiveTime xsi:type="IVL_TS">
                    <low nullFlavor="UNK"/>
                    <high nullFlavor="UNK"/>
                  </effectiveTime>
                  <effectiveTime operator="A" xsi:type="PIVL_TS">
                    <period unit="h" value="12"/>
                  </effectiveTime>
    
    ...
    
    </substanceAdministration>

    The effectiveTime Element is a part of substanceAdministration element.

    Wednesday, August 8, 2018 3:53 PM
  • And? Did you apply it to your XML structure? What is the problem?

    How could I have known this? Can you imagine that posting complete samples is necessary? Especially when namespaces are involved?

    You need elements instead of an array:

    namespace ConsoleCS
    {
        using System.Xml.Serialization;
    
        public class root
        {
            public SubstanceAdministration substanceAdministration { get; set; }        
        }
    
        public class SubstanceAdministration
        {
            [XmlAttribute()]
            public string classCode { get; set; }
            [XmlAttribute()]
            public string moodCode { get; set; }
            [XmlElement("templateId")]
            public TemplateId[] templateId { get; set; }
            public ID id { get; set; }
            public StatusCode statusCode { get; set; }        
            [XmlElement("effectiveTime")]
            [XmlElement(typeof(EffectiveTime_IVL_TS))]
            [XmlElement(typeof(EffectiveTime_PIVL_TS))]
            public BaseEffectiveTime[] effectiveTimes { get; set; }
        }
    
        [XmlType("IVL_TS")]
        public class EffectiveTime_IVL_TS : BaseEffectiveTime
        {
            public Low low { get; set; }
            public High high { get; set; }
        }
        [XmlType("PIVL_TS")]
        public class EffectiveTime_PIVL_TS : BaseEffectiveTime
        {
            public Period period { get; set; }
            [XmlAttribute()]
            public string @operator { get; set; }
        }
        public abstract class BaseEffectiveTime { }
    
        public class Period
        {
            [XmlAttribute()]
            public string unit { get; set; }
            [XmlAttribute()]
            public byte value { get; set; }
        }
    
        public class Low : NullFlavor { }
        public class High : NullFlavor { }
        public abstract class NullFlavor
        {
            [XmlAttribute()]
            public string nullFlavor { get; set; }
        }
    
        public class TemplateId
        {
            [XmlAttribute()]
            public string root { get; set; }
        }
    
        public class ID
        {
            [XmlAttribute()]
            public uint extension { get; set; }
            [XmlAttribute()]
            public string root { get; set; }
        }
    
        public class StatusCode
        {
            [XmlAttribute()]
            public string code { get; set; }
        }
    }
    
    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml.Serialization;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                root root;
                XmlSerializer serializer = new XmlSerializer(typeof(root));
                using (TextReader reader = new StringReader(RetrieveXml()))
                {
                    root = (root)serializer.Deserialize(reader);
                }
    
                Console.WriteLine(root.substanceAdministration.templateId[0].root);
                if (root.substanceAdministration.effectiveTimes != null)
                {
                    Console.WriteLine(root.substanceAdministration.effectiveTimes.Length);
                    Console.WriteLine(root.substanceAdministration.effectiveTimes[0].GetType().Name);
                    Console.WriteLine(root.substanceAdministration.effectiveTimes[1].GetType().Name);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[0] as EffectiveTime_IVL_TS).low.nullFlavor);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[0] as EffectiveTime_IVL_TS).high.nullFlavor);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[1] as EffectiveTime_PIVL_TS).@operator);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.unit);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.value);
                }
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private static string RetrieveXml()
            {
                return @"
                <root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                    <substanceAdministration classCode=""SBADM"" moodCode=""INT"">
    	                <templateId root=""2.16.840.1.113883.10.20.1.24""/>
    	                <templateId root=""1.3.6.1.4.1.12559.11.10.1.3.1.3.2""/>
    	                <id extension=""21957777"" root=""1.21.1""/>
    	                <statusCode code=""active""/>
    	                <effectiveTime xsi:type=""IVL_TS"">
    		                <low nullFlavor=""UNK""/>
    		                <high nullFlavor=""UNK""/>
    	                </effectiveTime>
    	                <effectiveTime operator=""A"" xsi:type=""PIVL_TS"">
    		                <period unit=""h"" value=""12""/>
    	                </effectiveTime>
                    </substanceAdministration>
                </root>
                ";
            }
        }
    }


    Wednesday, August 8, 2018 4:25 PM
  • And? Did you apply it to your XML structure? What is the problem?

    How could I have known this? Can you imagine that posting complete samples is necessary? Especially when namespaces are involved?

    You need elements instead of an array:

    namespace ConsoleCS
    {
        using System.Xml.Serialization;
    
        public class root
        {
            public SubstanceAdministration substanceAdministration { get; set; }        
        }
    
        public class SubstanceAdministration
        {
            [XmlAttributeAttribute()]
            public string classCode { get; set; }
            [XmlAttributeAttribute()]
            public string moodCode { get; set; }
            [XmlElementAttribute("templateId")]
            public TemplateId[] templateId { get; set; }
            public ID id { get; set; }
            public StatusCode statusCode { get; set; }        
            [XmlElement("effectiveTime")]
            [XmlElement(typeof(EffectiveTime_IVL_TS))]
            [XmlElement(typeof(EffectiveTime_PIVL_TS))]
            public BaseEffectiveTime[] effectiveTimes { get; set; }
        }
    
        [XmlType("IVL_TS")]
        public class EffectiveTime_IVL_TS : BaseEffectiveTime
        {
            public Low low { get; set; }
            public High high { get; set; }
        }
        [XmlType("PIVL_TS")]
        public class EffectiveTime_PIVL_TS : BaseEffectiveTime
        {
            public Period period { get; set; }
            [XmlAttribute()]
            public string @operator { get; set; }
        }
        public abstract class BaseEffectiveTime { }
    
        public class Period
        {
            [XmlAttribute()]
            public string unit { get; set; }
            [XmlAttribute()]
            public byte value { get; set; }
        }
    
        public class Low : NullFlavor { }
        public class High : NullFlavor { }
        public abstract class NullFlavor
        {
            [XmlAttribute()]
            public string nullFlavor { get; set; }
        }
    
        public class TemplateId
        {
            [XmlAttribute()]
            public string root { get; set; }
        }
    
        public class ID
        {
            [XmlAttribute()]
            public uint extension { get; set; }
            [XmlAttribute()]
            public string root { get; set; }
        }
    
        public class StatusCode
        {
            [XmlAttribute()]
            public string code { get; set; }
        }
    }
    
    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml.Serialization;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                root root;
                XmlSerializer serializer = new XmlSerializer(typeof(root));
                using (TextReader reader = new StringReader(RetrieveXml()))
                {
                    root = (root)serializer.Deserialize(reader);
                }
    
                Console.WriteLine(root.substanceAdministration.templateId[0].root);
                if (root.substanceAdministration.effectiveTimes != null)
                {
                    Console.WriteLine(root.substanceAdministration.effectiveTimes.Length);
                    Console.WriteLine(root.substanceAdministration.effectiveTimes[0].GetType().Name);
                    Console.WriteLine(root.substanceAdministration.effectiveTimes[1].GetType().Name);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[0] as EffectiveTime_IVL_TS).low.nullFlavor);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[0] as EffectiveTime_IVL_TS).high.nullFlavor);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[1] as EffectiveTime_PIVL_TS).@operator);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.unit);
                    Console.WriteLine((root.substanceAdministration.effectiveTimes[1] as EffectiveTime_PIVL_TS).period.value);
                }
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private static string RetrieveXml()
            {
                return @"
                <root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                    <substanceAdministration classCode=""SBADM"" moodCode=""INT"">
    	                <templateId root=""2.16.840.1.113883.10.20.1.24""/>
    	                <templateId root=""1.3.6.1.4.1.12559.11.10.1.3.1.3.2""/>
    	                <id extension=""21957777"" root=""1.21.1""/>
    	                <statusCode code=""active""/>
    	                <effectiveTime xsi:type=""IVL_TS"">
    		                <low nullFlavor=""UNK""/>
    		                <high nullFlavor=""UNK""/>
    	                </effectiveTime>
    	                <effectiveTime operator=""A"" xsi:type=""PIVL_TS"">
    		                <period unit=""h"" value=""12""/>
    	                </effectiveTime>
                    </substanceAdministration>
                </root>
                ";
            }
        }
    }

    My Class

    And namespaces is:

    <ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:epsos="urn:epsos-org:ep:medication" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    public class substanceAdministration { [XmlAttribute] public string classCode { get; set; } [XmlAttribute] public string moodCode { get; set; } [XmlElement] public templateId[] templateId { get; set; } [XmlElement] public id id { get; set; } [XmlElement] public statusCode statusCode { get; set; } [XmlArrayItem("effectiveTime", IsNullable = false)] [XmlArrayItem(typeof(effectiveTime))] [XmlArrayItem(typeof(_effectiveTime))] public BaseEffectiveTime[] effectiveTime { get; set; } [XmlElement] public doseQuantity doseQuantity { get; set; } [XmlElement] public rateQuantity rateQuantity { get; set; } [XmlElement] public consumable consumable { get; set; }

    }

    public abstract class BaseEffectiveTime { }

    [XmlType(TypeName = "IVL_TS")]
        public class effectiveTime: BaseEffectiveTime
        {
            [XmlElement("low")]
            public low low { get; set; }
            [XmlElement("high")]
            public high high { get; set; }
        }
        [XmlType(TypeName = "PIVL_TS")]
        public class _effectiveTime: BaseEffectiveTime
        {
            [XmlAttribute(AttributeName = "operator")]
            public string _operator { get; set; }
            [XmlElement]
            public period period { get; set; }
        }


    And after i deserialize:


    • Edited by DimitrisV12 Wednesday, August 8, 2018 5:03 PM
    Wednesday, August 8, 2018 5:00 PM
  • Well, then apply your namespace.
    Wednesday, August 8, 2018 6:00 PM
  • Even to declare the namespace nothing happened.

    --FIXED--

    I was wrong on xml annotation. If you can see the code of mine was [XmlArray...] but yours was [XmlElement]

    The problem solved. Thanks very much!

    • Edited by DimitrisV12 Thursday, August 9, 2018 12:11 PM Solved
    Thursday, August 9, 2018 9:02 AM
  • But, notice the huge quantity of types you had to design, just to write hteir values on screen... ROFL.

    Well, when you're intimidated by types, then you should program in a weaker typed language like JavaScript.

    I guess it's like cooking a steak. Sure, you can do it in a microwave oven. Lesser effort to get it done and lesser effort in cleaning the kitchen afterward. But is it advisable? I don't think so.

    Sunday, August 12, 2018 11:45 AM
  • In this case you must start your own thread and ask how to do it, mr UltraLowQualityPoints.

    Well, why don't you simply post your solution? Cause then you would help the OP, much more than I ever could?

    Monday, August 13, 2018 4:29 AM
  • How would posting a better solution be "hijacking"?
    Tuesday, August 14, 2018 7:07 PM
  • The OP is asking for XML code; you are askig for CSV code.

    May I remind you, that you brought up CSV as a solution in an XML-related thread.

    As it is okay in forums to demonstrate alternatives, the key is to demonstrate it. Not lament about being ignored.

    So, just do what every participant does: demonstrate your point. As this is a developer forum, this means post a concise and complete example.

    Wednesday, August 15, 2018 2:58 PM