none
XML serialisation from classes RRS feed

  • Question

  • Dear all,

    I need to generate part of an XML which is as below :

    ....
    <Labels>
       <Label1></Label1>
       <Label2></Label2>
       <Label3></Label3>
       <Label4></Label4>
       <Label5></Label5>
       <Label6></Label6>
    </Labels>

    Based on that I could create a classe name Lable and place define inside 6 public property name Label1 to Label6

    But it could happen that I would need more that 6 at later time.

    how can I define correctly my class in order to not be limited to number of label ?

    regards

    Thursday, May 17, 2018 7:40 AM

All replies

  • By using a List<T> to hold the Label objects . You can add Label object to the collection and serialize the List<T>.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/examples-of-xml-serialization

    Thursday, May 17, 2018 2:34 PM
  • Create Labels class with List<string> Label

    [Serializable]
    public class Labels
    {
    	[XmlElement("Label")]
    	public List<string> Label { get; set; }
    }

    Populate class Labels and Serialize class Labels to create desired XML.

    XmlSerializer xsSubmit = new XmlSerializer(typeof(Labels));


    Thanks, AT

    Thursday, May 17, 2018 3:11 PM
  • Agreed but in my XAML file output I need to have entries as Label1, label2,label3.
    Each label name need to be unique as it will be used as a field for reporting.

    So that does not solve my issue, any other idea?

    Friday, May 18, 2018 6:48 AM
  • Hello wakefun,

    Try to implement IXmlSerializable interface and build custom serialize process.

    [Serializable]
        public class Labels : IXmlSerializable
        {
            public Dictionary<string, string> Label { get; set; }
            public XmlSchema GetSchema()
            {return new XmlSchema();}
    
            public void ReadXml(XmlReader reader)
            { }
    
            public void WriteXml(XmlWriter writer)
            {        
                foreach (var keyvalue in Label)
                {
                    //prevent occuring closing tages like <lable1 /> when no data is present   
                    writer.WriteElementString(keyvalue.Key, keyvalue.Value);               
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                XmlSerializer xsSubmit = new XmlSerializer(typeof(Labels));
                TextWriter writer = new StreamWriter("T1.txt");
                Labels labels = new Labels { Label = new Dictionary<string, string>() {
                    { "lable1","1"},
                    { "Lable2","2"},
                    { "Lable3","3"}  }};
                xsSubmit.Serialize(writer, labels);
            }
        }

    Output:

    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 7:15 AM
    Moderator