none
XML attribute serialisation issue RRS feed

  • Question

  • Dear all, 

    I have the folloing set of classes :

    public class Info: Ilnfo
    	{
    		[XmlAttribute("Phase")]
    		public string Phase { get; set; }
    
    		[XmlElement("Element")]
    		public Data Element { get; set; }
    		[XmlElement("Alias")]
    		public Data Alias { get; set; }
    		[XmlElement("Condition")]
    		public Data AnalyticalCondition { get; set; }
    		
    	}
    	//This class handle chanel data properties and serve as a base class type for ChanelInfo
    	public class Data: IDataAttribute
    	{
    		[XmlAttribute("RGB")]
    		public string ColorRGB { get; set; }
    		
    		[XmlAttribute("Label")]
    		public string Label { get; set; }
    
    		[XmlText]
    		public string Value { get; set; }
    	}

    Which produce the folling XML output:

    <ChanelInfo>
              <Element RGB="207,172,112" Label="Elt">Fe</Element>
              <Alias RGB="207,172,112" Label="AliasName">Fe_02</Alias>
              <Condition RGB="207,172,112" Label="AC">TH-315</Condition>
            </ChanelInfo>

    As you can see the [XmlAttribute("Phase")] is missing from ChanelInfo

    Any idea what I am doing wrong ?

    regards

    Friday, May 18, 2018 2:13 PM

Answers

  • There are no null attributes in XML, thus you need to assign a value for the attribute to be present in the output.

    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml.Serialization;
    
        public class Info
        {
            [XmlAttribute]
            public string Phase { get; set; }
            public Data Element { get; set; }
            public Data Alias { get; set; }
            [XmlElement("Condition")]
            public Data AnalyticalCondition { get; set; }
        }
    
        [XmlRoot("Info")]
        public class InfoEnforcePhase
        {
            private string phase = string.Empty;
            [XmlAttribute]
            public string Phase
            {
                get { return this.phase; }
                set { if (value != null && value != this.phase) this.phase = value; }
            }
            public Data Element { get; set; }
            public Data Alias { get; set; }
            [XmlElement("Condition")]
            public Data AnalyticalCondition { get; set; }
        }
    
        public class Data
        {
            [XmlAttribute("RGB")]
            public string ColorRGB { get; set; }
            [XmlAttribute]
            public string Label { get; set; }
            [XmlText]
            public string Value { get; set; }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Info info;
    
                info = MockInfoWithPhase();
                Console.WriteLine("With Phase\n");
                Console.WriteLine(SerializeAsXml<Info>(info));
    
                info = MockInfoWithoutPhase();
                Console.WriteLine("\nWithout Phase\n");
                Console.WriteLine(SerializeAsXml<Info>(info));
    
                InfoEnforcePhase infoEnforcePhase = MockInfoEnforcePhase();
                Console.WriteLine("\nEnforce Phase\n");
                Console.WriteLine(SerializeAsXml<InfoEnforcePhase>(infoEnforcePhase));
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
    
            private static InfoEnforcePhase MockInfoEnforcePhase()
            {
                InfoEnforcePhase info = new InfoEnforcePhase();
                info.Alias = new Data() { ColorRGB = "2", Label = "3", Value = "4" };
                info.Element = new Data() { ColorRGB = "5", Label = "6", Value = "7" };
                info.AnalyticalCondition = new Data() { ColorRGB = "8", Label = "9", Value = "10" };
                return info;
            }
    
            private static Info MockInfoWithPhase()
            {
                Info info = new Info() { Phase = "1" };
                info.Alias = new Data() { ColorRGB = "2", Label = "3", Value = "4" };
                info.Element = new Data() { ColorRGB = "5", Label = "6", Value = "7" };
                info.AnalyticalCondition = new Data() { ColorRGB = "8", Label = "9", Value = "10" };
                return info;
            }
    
            private static Info MockInfoWithoutPhase()
            {
                Info info = new Info();
                info.Alias = new Data() { ColorRGB = "2", Label = "3", Value = "4" };
                info.Element = new Data() { ColorRGB = "5", Label = "6", Value = "7" };
                info.AnalyticalCondition = new Data() { ColorRGB = "8", Label = "9", Value = "10" };
                return info;
            }
    
            private static string SerializeAsXml<T>(T info)
            {
                XmlSerializer ser = new XmlSerializer(typeof(T));
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                using (MemoryStream writer = new MemoryStream())
                {
                    ser.Serialize(writer, info, ns);
                    writer.Position = 0;
                    using (StreamReader reader = new StreamReader(writer))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
        }
    }

    Result:

    With Phase
    
    <?xml version="1.0"?>
    <Info Phase="1">
      <Element RGB="5" Label="6">7</Element>
      <Alias RGB="2" Label="3">4</Alias>
      <Condition RGB="8" Label="9">10</Condition>
    </Info>
    
    Without Phase
    
    <?xml version="1.0"?>
    <Info>
      <Element RGB="5" Label="6">7</Element>
      <Alias RGB="2" Label="3">4</Alias>
      <Condition RGB="8" Label="9">10</Condition>
    </Info>
    
    Enforce Phase
    
    <?xml version="1.0"?>
    <Info Phase="">
      <Element RGB="5" Label="6">7</Element>
      <Alias RGB="2" Label="3">4</Alias>
      <Condition RGB="8" Label="9">10</Condition>
    </Info>
    
    Done.




    • Edited by Stefan Hoffmann Sunday, May 20, 2018 12:36 PM
    • Marked as answer by wakefun Sunday, May 20, 2018 1:52 PM
    Sunday, May 20, 2018 12:17 PM