-1

I want to serialize the following from C# classes/structures into xml:

XML sample:

<xml>
   <somename id="bla">content</somename>
</xml>

How can I achieve, that a string has an additional attribute called id?

Robert Wolf
  • 191
  • 1
  • 11
  • This may help https://stackoverflow.com/questions/11330643/serialize-property-as-xml-attribute-in-element/11330786 – Serg Feb 03 '22 at 13:14

1 Answers1

2

I would use something like this:

    [XmlRoot(ElementName="somename")]
    public class Somename {
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName="xml")]
    public class Xml {
        [XmlElement(ElementName="somename")]
        public Somename Somename { get; set; }
    }

Actually you can use this tool https://xmltocsharp.azurewebsites.net/

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109