none
HOw to represent XML attribute in my Class used for serialisation RRS feed

  • Question

  • Dear all,

    I have the following XML output to reproduce :

    <Info Id="1" Mode="1">
      <Data1 label="First data"></Data1>
      <Data2 label="First data"></Data2>
    </Info>

    For representing my class I would define it as below :

    [XmlRoot(ElementName = "Info")]
     public class ReportInfo
    	{
    		public string Data1 { get; set; }
    		public string Data2 { get; set; }
    	}

    The class properties will allows me to generate XML element Data1 and Data1.

    But what I am missing is how can I define my class in order that it produce also the attribute Id and Mode of the Info root node

    The value of the attreribute ID and Mode will be populated dynamically through a variable

    Hoy can I change my class defienition to support ID and Mode serialzed as XML attribute instead of XML Element ?

    Regarsd

    Thursday, May 17, 2018 7:55 AM

Answers

  • E.g. using the XmlText and XmlAttribute annotation on your data class:

    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml.Serialization;
    
        public class Data
        {
            [XmlAttribute(AttributeName = "label")]
            public string Label { get; set; }
            [XmlText]
            public string Value { get; set; }
        }
            
        public class Info
        {
            [XmlAttribute]
            public string Id { get; set; }
            [XmlAttribute]
            public string Mode { get; set; }
            public Data Data1 { get; set; }
            public Data Data2 { get; set; }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Info info = MockInfo();
                SerializeAsXml(info);
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private static Info MockInfo()
            {
                Info info = new Info() { Id = "1", Mode = "2" };
                info.Data1 = new Data() { Label = "3", Value = "4" };
                info.Data2 = new Data() { Label = "5", Value = "6" };
                return info;
            }
    
            private static void SerializeAsXml(Info info)
            {
                string filename = @"C:\Temp\Test.xml";
                XmlSerializer ser = new XmlSerializer(typeof(Info));
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                using (TextWriter writer = new StreamWriter(filename))
                {
                    ser.Serialize(writer, info, ns);
                }
            }
        }
    }
    
    

    results in

    <?xml version="1.0" encoding="utf-8"?>
    <Info Id="1" Mode="2">
      <Data1 label="3">4</Data1>
      <Data2 label="5">6</Data2>
    </Info>




    • Edited by Stefan Hoffmann Thursday, May 17, 2018 12:51 PM code clean-up
    • Proposed as answer by cheong00Editor Thursday, May 17, 2018 2:43 PM
    • Marked as answer by wakefun Friday, May 18, 2018 6:44 AM
    Thursday, May 17, 2018 12:37 PM

All replies

  • You need XmlAttributeAttribute.

    https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlattributeattribute?view=netframework-4.7.2


    [XmlAttribute]
    public int Id { get; set; }
    [XmlAttribute]
    public int Mode { get; set; }

    Thursday, May 17, 2018 10:15 AM
    Answerer
  • This is ok for the class which became as below :

    XmlRoot(ElementName = "Info")] public class ReportInfo {

    [XmlAttribute] public int Id { get; set; } [XmlAttribute] public int Mode { get; set; }
    public string Data1 { get; set; } public string Data2 { get; set; } }

    But then as you can see my Data1 and Data2 element in XML that I try to produce have also an attribute name Label.

    Hoy can I change my class definition in order that Label attributes gets added to Data1 and Data2 property ?

    regards

    Thursday, May 17, 2018 11:57 AM
  • E.g. using the XmlText and XmlAttribute annotation on your data class:

    namespace ConsoleCS
    {
        using System;
        using System.IO;
        using System.Xml.Serialization;
    
        public class Data
        {
            [XmlAttribute(AttributeName = "label")]
            public string Label { get; set; }
            [XmlText]
            public string Value { get; set; }
        }
            
        public class Info
        {
            [XmlAttribute]
            public string Id { get; set; }
            [XmlAttribute]
            public string Mode { get; set; }
            public Data Data1 { get; set; }
            public Data Data2 { get; set; }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Info info = MockInfo();
                SerializeAsXml(info);
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private static Info MockInfo()
            {
                Info info = new Info() { Id = "1", Mode = "2" };
                info.Data1 = new Data() { Label = "3", Value = "4" };
                info.Data2 = new Data() { Label = "5", Value = "6" };
                return info;
            }
    
            private static void SerializeAsXml(Info info)
            {
                string filename = @"C:\Temp\Test.xml";
                XmlSerializer ser = new XmlSerializer(typeof(Info));
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                using (TextWriter writer = new StreamWriter(filename))
                {
                    ser.Serialize(writer, info, ns);
                }
            }
        }
    }
    
    

    results in

    <?xml version="1.0" encoding="utf-8"?>
    <Info Id="1" Mode="2">
      <Data1 label="3">4</Data1>
      <Data2 label="5">6</Data2>
    </Info>




    • Edited by Stefan Hoffmann Thursday, May 17, 2018 12:51 PM code clean-up
    • Proposed as answer by cheong00Editor Thursday, May 17, 2018 2:43 PM
    • Marked as answer by wakefun Friday, May 18, 2018 6:44 AM
    Thursday, May 17, 2018 12:37 PM
  • tnaks so much.
    Thursday, May 17, 2018 1:23 PM
  • Hello wakefun,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 18, 2018 5:21 AM
    Moderator